2013-07-11 11 views

risposta

17

ho risolto questo con la seguente

setLocal EnableDelayedExpansion 

set variable1=this is variable1 
set variable2=is 
set variable3=test 

if not "x!variable1:%variable2%=!"=="x%variable1%" (
    echo YES 
) else (
    echo NO 
) 

if not "x!variable1:%variable3%=!"=="x%variable1%" (
    echo YES 
) else (
    echo NO 
) 

endlocal 

ho avuto l'idea di base dalla seguente risposta, ma non cercava una variabile, quindi non era completamente quello che stavo cercando.

Batch file: Find if substring is in string (not in a file)

4

altro modo:

echo/%variable1%|find "%variable2%" >nul 
if %errorlevel% == 0 (echo yes) else (echo no) 

il / impedisce uscita Echo is ON o Echo is OFF in caso %variable1% è vuoto.

+0

Questo non riesce per variabile1 = '/'? , variable1 = 'on' e variable1 =' off'. Considerare "echo foobar% variable1% | find"% variable2% "> nul', che non riesce per variable2 = foobar. – GKFX

1

La risposta di Gary Brunton non ha funzionato per me.

Se si prova con set variable1="C:\Users\My Name\", vi ritroverete con un errore:

'Name\""' is not recognized as an internal or external command 

Adattare questa risposta Find out whether an environment variable contains a substring, ho finito con:

echo.%variable1%|findstr /C:"%variable2%" >nul 2>&1 
if not errorlevel 1 (
    echo Found 
) else (
    echo Not found 
) 
Problemi correlati