1

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)

Community
  • 1
  • 1
MAbraham1
  • 1,717
  • 4
  • 28
  • 45

2 Answers2

8

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
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • The second answer here helped me out, I had square brackets in the string and managed to get things working with this answer. – Chef Pharaoh Apr 24 '15 at 22:50
3

The solution is to use FindStr and the NULL redirect, >nul.

SET var=%1
SET searchVal=Tomcat
SET var|FINDSTR /b "var="|FINDSTR /i %searchVal% >nul
IF ERRORLEVEL 1 (echo It does't contain Tomcat) ELSE (echo It contains Tomcat)

Save as test.bat and execute with the parameter to be searched, as follows: test Tomcat7

C:\>test Tomcat9
It contains Tomcat
MAbraham1
  • 1,717
  • 4
  • 28
  • 45