Searching for a string of text in an MS-DOS batch file
Using the findstr command lets you search for text within any plaintext file. Using this command within a batch file lets you search for text and create events off the results found. Below are examples.
In the example below, this basic batch file searches through the hope.txt file for the string computerhope and, if found, echo's back There is hope!.
@echo off findstr /m "computerhope" hope.txt if %errorlevel%==0 ( echo There is hope! )
Log results and wildcards
In the example below, this batch file searches for computerhope in any txt file in the current directory using the wildcards *.txt. It prints any files found containing this string into the results.txt file. Also, this batch file has an else statement that prints if no matches were found.
When doing the "else," it *must* follow the close parenthesis. It must be ") else (" or you'll get the 'else' is not recognized as an internal or external command, operable program, or batch file error.
@echo off findstr /m "computerhope" *.txt > results.txt if %errorlevel%==0 ( echo Found! logged files into results.txt ) else ( echo No matches found )