20

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Manjot
  • 11,166
  • 9
  • 38
  • 49
  • Does `find` return different `errorlevel` values depending on whether the string is found? If so, there's your solution right there. – C. K. Young Jan 06 '10 at 00:50
  • 3
    Do you really mean MS-DOS or do you mean the cmd.exe batch interpreter on Windows? – nobody Jan 06 '10 at 01:19

6 Answers6

29

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
  • 236,483
  • 35
  • 423
  • 525
  • It's better to use `if errorlevel 1`. It has slightly different semantics, though, but is overall more robust. – Joey Apr 12 '11 at 04:56
10
C:\test>find /c "string" file | find ": 0" 1>nul && echo "execute command here"
Spikolynn
  • 4,067
  • 2
  • 37
  • 44
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • 2
    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 Jan 06 '10 at 01:36
  • I edited the answer to add the /c switch. Otherwise 'find' does not report count (": 0") – Spikolynn Nov 16 '17 at 11:44
8
@echo off
cls
MD %homedrive%\TEMPBBDVD\
CLS
TIMEOUT /T 1 >NUL
CLS
systeminfo >%homedrive%\TEMPBBDVD\info.txt
cls
timeout /t 3 >nul
cls
find "x64-based PC" %homedrive%\TEMPBBDVD\info.txt >nul
if %errorlevel% equ 1 goto 32bitsok
goto 64bitsok
cls

:commandlineerror
cls
echo error, command failed or you not are using windows OS.
pause >nul
cls
exit

:64bitsok
cls
echo done, system of 64 bits
pause >nul
cls
del /q /f %homedrive%\TEMPBBDVD\info.txt >nul
cls
timeout /t 1 >nul
cls
RD %homedrive%\TEMPBBDVD\ >nul
cls
exit

:32bitsok
cls
echo done, system of 32 bits
pause >nul
cls
del /q /f %homedrive%\TEMPBBDVD\info.txt >nul
cls
timeout /t 1 >nul
cls
RD %homedrive%\TEMPBBDVD\ >nul
cls
exit
Ozan
  • 4,345
  • 2
  • 23
  • 35
HELPY
  • 112
  • 1
  • 1
7

We have two commands, first is "condition_command", second is "result_command". If we need run "result_command" when "condition_command" is successful (errorlevel=0):

condition_command && result_command

If we need run "result_command" when "condition_command" is fail:

condition_command || result_command

Therefore for run "some_command" in case when we have "string" in the file "status.txt":

find "string" status.txt 1>nul && some_command

in case when we have not "string" in the file "status.txt":

find "string" status.txt 1>nul || some_command
snv.dev
  • 101
  • 1
  • 2
6

As the answer is marked correct then it's a Windows Dos prompt script and this will work too:

find "string" status.txt >nul && call "my batch file.bat"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
-1

That's more straightforward:

find /c "string" file
if %errorlevel% not equ 1 goto something