Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOS: find a string, if found then run another script

Tags:

I want to find a string in a file using DOS:

For example

find "string" status.txt

And when it is found, I want to run a batch file.

What is the best way to do this?

like image 357
Manjot Avatar asked Jan 06 '10 00:01

Manjot


2 Answers

It's been awhile since I've done anything with batch files but I think that the following works:

find /c "string" file
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo notfound
goto done
:done

This is really a proof of concept; clean up as it suits your needs. The key is that find returns an errorlevel of 1 if string is not in file. We branch to notfound in this case otherwise we handle the found case.

like image 186
jason Avatar answered Oct 02 '22 00:10

jason


C:\test>find /c "string" file | find ": 0" 1>nul && echo "execute command here"
like image 23
ghostdog74 Avatar answered Oct 01 '22 23:10

ghostdog74