user2022089
user2022089

Reputation: 39

Batch file to look in a file and run program

Alright, I believe I'm pushing the limitations of batch files with this one.

I need a batch file that will look in a file and find "X" or "y". If either is found, to run a program. If neither is found, continue on with the rest of the code. The file that it will look in has the extension .inf. It's opened using note pad. I'm not even sure where to begin. Any help would be greatly appreciated. :)

Upvotes: 0

Views: 257

Answers (3)

Andriy M
Andriy M

Reputation: 77657

You can use FINDSTR to search for multiple entries at the same time. Use it like this:

FINDSTR "term1 term2 term3 ..."

The result will be a success if at least one term has been found. Use the /I switch to make the search case insensitive, if necessary:

FINDSTR /I "term1 term2 term3 ..."

FINDSTR searches stdin by default. Redirect the input to your .inf file to make it search the file:

FINDSTR /I "term1 term2 term3 ..." <file.inf

Alternatively, you could put the file name as another parameter:

FINDSTR /I "term1 term2 term3 ..." file.inf

The output will be slightly different in the two cases, but I understand you don't actually need the output but the result of the search, i.e. whether it succeeded or failed.

To check the result, you can use explicit ERRORLEVEL test, like this:

FINDSTR /I "term1 term2 term3 ..." file.inf
IF NOT ERRORLEVEL 1 yourprogram.exe

An alternative syntax to that would be to use the ERRORLEVEL system variable, which is probably more straightforward than the former:

IF %ERRORLEVEL% == 0 yourprogram.exe

Another way to do the same is to use the && operator. This method is not entirely identical to the ERRORLEVEL test, but with FINDSTR both ERRORLEVEL test and && work identically, and in this situation the && method has the advantage of being more concise:

FINDSTR /I "term1 term2 term3 ..." file.inf && yourprogram.exe

This is almost it. One final note is, since you may not be actually interested in the output of FINDSTR, you might as well want to suppress it by redirecting it to NUL, like this:

FINDSTR /I "term1 term2 term3 ..." file.inf >NUL && yourprogram.exe

Upvotes: 2

Hafthor
Hafthor

Reputation: 16896

A combination of FIND and FC can do it.

@echo off
REM try to find X
FIND /c "X" file.inf >test_isxfound.txt
REM what does it look like when I don't find a match
FIND /c "th1s$tringb3tt3rn0tbeinthisfile" file.inf >test_xnotfound.txt
REM then compare those results with the results where we know it wasn't found
FC test_xnotfound.txt test_isxfound.txt
REM then check to see if FC saw a difference
IF ERRORLEVEL 1 goto xisfound

ECHO *** X is not found
goto end
:xisfound
ECHO *** X is found
goto end
:end
del test_xnotfound.txt
del test_isxfound.txt

Upvotes: 0

Onorio Catenacci
Onorio Catenacci

Reputation: 15293

Try starting with this page:

http://www.robvanderwoude.com/findstr.php

Then for further reference on the basics of batch files:

http://www.robvanderwoude.com/batchfiles.php

Upvotes: 0

Related Questions