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

set "ol=%1"
set handledString=!ol:%2=!
IF %1 == %handledString% GOTO wayone
IF %1 NEQ %handledString% GOTO waytwo

echo."substring not found"
goto end

echo."substring found"
goto end


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