Batch file: Find if substring is in string (not in a file) Part 2 - Using Variables

MAbraham1 picture MAbraham1 · Jul 11, 2013 · Viewed 29.3k times · Source

In a Windows batch file, I have a string 'abcdefg'. I want to check if 'bcd' is in the string, but I also want each to be in a variable, or pass in a parameter for the string.

This solution comes close, but uses constants rather than variables. Batch file: Find if substring is in string (not in a file)

Answer

Endoro picture Endoro · Jul 11, 2013

try one:

set "var=abcdefg"
set "search=bcd"
CALL set "test=%%var:%search%=%%"
if "%test%"=="%var%" (echo %search% is not in %var%) else echo %search% in %var% found


set "var=abcdefg"
set "search=bcd"
echo %var%|findstr /lic:"%search%" >nul && echo %search% found || echo %search% not found