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)

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

The second answer here helped me out, I had square brackets in the string and managed to get things working with this answer.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.