2013-07-11 3 views

Odpowiedz

17

Mam rozwiązane to z następującymi

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 

mam podstawową ideę z następującej odpowiedzi, ale nie było to wyszukiwanie według zmiennej, więc nie było to całkowicie to, czego szukałem.

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

4

inny sposób:

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

/ uniemożliwia wyjście Echo is ON lub Echo is OFF w przypadku %variable1% jest pusta.

+0

to zawiedzie dla variable1 = '/'? , variable1 = 'on', i variable1 =' off'. Rozważmy "echo foobar% zmienna1% | znajdź"% zmienna2% "> nul', które nie działa dla zmiennej2 = foobar. – GKFX

1

Odpowiedź Gary Bruntona nie zadziałała.

Jeśli spróbujesz z set variable1="C:\Users\My Name\", skończy się z błędem:

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

Adaptacja tę odpowiedź Find out whether an environment variable contains a substring, skończyło się z:

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