如何检查变量是否包含Windows批处理文件中的另一个变量?

Gar*_*ton 16 windows batch-file

假设有以下批处理文件

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

if variable1 contains variable2 (
    echo YES
) else (
    echo NO
)

if variable1 contains variable3 (
    echo YES
) else (
    echo NO
)
Run Code Online (Sandbox Code Playgroud)

我希望输出为YES,然后是NO

Gar*_*ton 20

我用以下方法解决了这个问题

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
Run Code Online (Sandbox Code Playgroud)

我从以下答案中得到了基本的想法,但它不是通过变量进行搜索,因此它并不完全是我想要的.

批处理文件:查找substring是否在字符串中(不在文件中)


Ste*_*han 6

其他方式:

echo/%variable1%|find "%variable2%" >nul
if %errorlevel% == 0 (echo yes) else (echo no)
Run Code Online (Sandbox Code Playgroud)

所述/的输出防止Echo is ONEcho is OFF情况%variable1%是空的.