This code can be implemented in HostMonitor as a Shell Script test.
HostMonitor Startcmd: cmd /c %Script% %Params%
Internal Syntax: script {file(s) to scan, allows wildcards} {search string}
Example:
Params: C:\*.txt error message
- will report all files txt-files in C:\ that contain the string "error message"
Params: C:\filename.* error message
- will report all files with filename "filename", regardless of extention, in C:\ that contain the string "error message"
Do not forget to comment the first method and uncomment the second before running the script.
Example:
Params: C:\filename.* "error message that I want to find in at least one of the files"
- will report all files with filename "filename", regardless of extention, in C:\ that contain the string mentioned
Code: Select all
@echo off
:--------------------------------------------------------
:: Jump to Help if not all parameters have been given
IF "%~1"=="" GOTO HELP
IF "%~2"=="" GOTO HELP
:--------------------------------------------------------
:--------------------------------------------------------
:: Initiate variables
:--------------------------------------------------------
SET SearchString=
SET InputStr=
SET ConcatFiles=
SET ProcessFile=
SET PreviousFile=
SET ProcessingResult=
SET TmpDrivePath=
SET TmpFile=
:--------------------------------------------------------
:========================================================
: Make a choice for one of the two methods below to get the searchString
: 1. Search for up to 8 words in a string by just typing them without quotation marks
: The fun thing of this method is that you can just type along, without having to worry about quotation marks!
: example: script c:\*.txt text to search for
: 2. If you need to search for strings that might be longer than 8 words, comment method 1 and uncomment method 2.
: You will have to use quotation marks now!
: If you don't mind using quotation marks you can always use this method of course
: example: script c:*.txt "text to search for that may be longer than 8 words"
:========================================================
:--------------------------------------------------------
:: 1. concatenate strings up to 8 items as search string and clean leading and trailing spaces if less items are supplied
SET InputStr=%2 %3 %4 %5 %6 %7 %8 %9
FOR /f "tokens=* delims= " %%a IN ("%InputStr%") DO SET InputStr=%%a
SET InputStr=%InputStr%##
SET InputStr=%InputStr: ##=##%
SET InputStr=%InputStr: ##=##%
SET InputStr=%InputStr: ##=##%
SET InputStr=%InputStr: ##=##%
SET InputStr=%InputStr:##=%
SET SearchString=%InputStr%
:--------------------------------------------------------
:: 2. Uncomment this (and comment the section above!) if you want to search for long strings
: SET SearchString=%~2
:--------------------------------------------------------
:--------------------------------------------------------
:: Loop through the files (%1) and search for the string (%SearchString%)
:--------------------------------------------------------
FOR %%f IN (%1) DO FOR /F "tokens=1 delims= " %%e IN ('type %%f ^| find /I "%SearchString%"') DO (
SET ProcessFile=%%f
SET TmpDrivePath=%%~dpf
SET TmpFile=%%~nxf
CALL :CHECKITEMS
)
:--------------------------------------------------------
:--------------------------------------------------------
:: Looping is done, now jump to build the report
:--------------------------------------------------------
GOTO :MAKEREPORT
:--------------------------------------------------------
:--------------------------------------------------------
:: This section is called from the loop
:--------------------------------------------------------
:CHECKITEMS
IF "%PreviousFile%"=="" GOTO GETFIRST
IF "%ProcessFile%"=="%PreviousFile%" GOTO TRYNEXT
GOTO :GETSECOND
:GETFIRST
COPY %ProcessFile% %TmpDrivePath%ERROR_%TmpFile%_ERROR > NUL
SET ConcatFiles=%ProcessFile%
GOTO :TRYNEXT
:GETSECOND
COPY %ProcessFile% %TmpDrivePath%ERROR_%TmpFile%_ERROR > NUL
DEL %PreviousFile%
SET ConcatFiles=%ConcatFiles%, %ProcessFile%
GOTO :TRYNEXT
:TRYNEXT
SET PreviousFile=%ProcessFile%
GOTO :EOF
:--------------------------------------------------------
:--------------------------------------------------------
:: Build the report
:MAKEREPORT
IF "%ConcatFiles%"=="" (
SET ProcessingResult=Ok:No instances found
) ELSE (
SET ProcessingResult=Bad:Errors found in files: %ConcatFiles%
)
ECHO ScriptRes:%ProcessingResult%
:--------------------------------------------------------
:--------------------------------------------------------
:: Clear the variables that have been used and delete the last processed file
IF NOT "%ProcessFile%"=="" DEL %ProcessFile%
SET SearchString=
SET InputStr=
SET ConcatFiles=
SET ProcessFile=
SET PreviousFile=
SET ProcessingResult=
SET TmpDrivePath=
SET TmpFile=
GOTO END
:--------------------------------------------------------
:--------------------------------------------------------
:HELP
ECHO ScriptRes:Unknown:Usage: script {filepath to search, may include wildcard} {string to search for}
GOTO END
:--------------------------------------------------------
:--------------------------------------------------------
:END
:--------------------------------------------------------
Have fun!
Gijsbert van Oeveren