2013-07-11 31 views

回答

17

輸出,我用下面的

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 

解決了這個給我的基本思路從下面的答案,但它不是由一個變量搜索,所以它不完全是我正在尋找。

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

4

另一種方式:

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

/防止Echo is ONEcho is OFF輸出的情況下%variable1%是空的。

+0

這失敗的變量1 ='/'? ,variable1 ='on',variable1 ='off'。考慮'echo foobar%variable1%|找到「%variable2%」> nul',這對variable2 = foobar失敗。 – GKFX

1

加里布倫頓的答案不適合我。

如果用set variable1="C:\Users\My Name\"嘗試,你將最終獲得一個錯誤:

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

適應這個答案Find out whether an environment variable contains a substring,我結束了:

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