Triggering a Batch File with findstr

It is possible to write a batch file that conditionally takes action based on whether findstr detects a certain error string. This technique uses the Windows NT conditional processing commands: && and ||. The following command executes the batch file Batch1.bat if any of the SQL error strings in the file Search.txt are found:

findstr /i /g:search.txt errorlog*.* && batch1

Batch1.bat could invoke a program or run an ISQL/w job that uses the extended stored procedure xp_sendmail to send a mail message notifying an operator of a problem.

The above techniques can be combined to perform a variety of tasks. For example, the following batch file will continuously run a findstr command that conditionally executes Batch1.bat if any of the SQL error strings in the file Search.txt are found, excluding the strings in Exclude.txt:

echo off
:start
findstr /i /g:search.txt errorlog*.* | findstr
/v /i /g:exclude.txt && (echo WARNING & batch1)
goto start

With a little experimentation, these techniques can reduce the labor involved in searching SQL Server error log files and DBCC output files. These same techniques can also be used for any general text-searching task, not just those related to SQL Server.

Note The Windows NT Resource Kit includes the powerful Perl scripting language, beginning with Windows NT 3.51. It's specifically designed for file scanning and batch automation. If you need more advanced batch automation and error-handling capabilities, you should strongly consider the Windows NT Resource Kit and Perl.

Starting with Windows NT 4.0, several powerful enhancements were added to the Cmd.exe command processor file that can facilitate writing patch jobs, especially those requiring conditional operations. One example is the FOR command. For details, run FOR/? or CMD/?.