Friday, June 17, 2011

How to check if sub-string belongs to the string in batch script

Check new series of the articles. Review and user experience on test management systems. Functionality and usability.
-------------------

Recently I was in need to prepare the batch file taking the sting and some pattern as the parameters. They required that file to check if that string contains the certain substring and basing on the result execute either one functionality or another. So the solution is below:

rem first parameter should hold the main string
rem second one should hold the substring to be found

SETLOCAL ENABLEDELAYEDEXPANSION 
set "ol=%1"
set handledString=!ol:%2=!
echo."%handledString%"
IF %1 == %handledString% GOTO wayone
IF %1 NEQ %handledString% GOTO waytwo

:wayone
echo."substring not found"
goto end

:waytwo
echo."substring found"
goto end

:end

UPD: Here is more sophisticated way how to do this.