Marwan Jaber
Marwan Jaber

Reputation: 641

bat file script to check if string contains other string

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

Upvotes: 3

Views: 6527

Answers (1)

MC ND
MC ND

Reputation: 70961

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

Upvotes: 1

Related Questions