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?

2
  • Does find return different errorlevel values depending on whether the string is found? If so, there's your solution right there. Commented Jan 6, 2010 at 0:50
  • 3
    Do you really mean MS-DOS or do you mean the cmd.exe batch interpreter on Windows? Commented Jan 6, 2010 at 1:19

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's better to use if errorlevel 1. It has slightly different semantics, though, but is overall more robust.
10
C:\test>find /c "string" file | find ": 0" 1>nul && echo "execute command here"

2 Comments

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.
I edited the answer to add the /c switch. Otherwise 'find' does not report count (": 0")
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

Comments

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

Comments

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"

2 Comments

access denied on windows
@Jack That's a problem with your permissions on the files or folder you are using.
-1

That's more straightforward:

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

1 Comment

gives error not was unexpected at this time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.