tags:

views:

412

answers:

2

Hi,

I want to find a string in a file using dos.

e.g.

find "string" status.txt

And when it has found it, i was to run a batch file (like an if-else statement). How can I do it?

Regards

Manjot

+2  A: 

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.

Jason
ThanksThis helped a lot
Manjot
+3  A: 
C:\test>find "string" file | find ": 0" 1>nul && echo "execute command here"
ghostdog74
This was my initial approach but I don't like relying on the output of `find` henceforth containing `: 0`. I couldn't find (pun intended) documentation supporting this and think the `errorlevel` approach might be more future proof.
Jason