bat file script to check if string contains other

2020-07-23 04:15发布

I need to write a batch file that will check if a variable contains specific value. I tried to do the following:

If "%%a"=="%%a:%pattern%" (
    echo Yes
) else (
   echo No
)

input example: %%a="bob binson" %patern%="binson"

I never get Yes printed! can anyone please tell what i missed or give an example of how (s)he would do it?

Thanks in Advance

1条回答
对你真心纯属浪费
2楼-- · 2020-07-23 05:07

Substring operations are not available in for replaceable parameters. You need to assign the data to a variable and then execute the operation on this variable

@echo off
    setlocal enableextensions disabledelayedexpansion

    >"tempFile" (
        echo bob binson
        echo ted jones
        echo binson
    )

    set "pattern=binson"

    for /f "usebackq delims=" %%a in ("tempFile") do (
        echo data: %%a

        set "line=%%a"
        setlocal enabledelayedexpansion
        if "!line:%pattern%=!"=="!line!" (
            echo .... pattern not found
        ) else (
            echo .... pattern found
        )
        endlocal
    )

    del /q tempFile
查看更多
登录 后发表回答