KS-Soft. Network Management Solutions
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister    ProfileProfile    Log inLog in 

Check if a string of words is found in a set of files

 
Post new topic   Reply to topic    KS-Soft Forum Index -> Library
View previous topic :: View next topic  
Author Message
GijsbertVanOeveren



Joined: 03 Jan 2008
Posts: 11

PostPosted: Mon Jan 14, 2008 9:23 am    Post subject: Check if a string of words is found in a set of files Reply with quote

The following batch code for NT will help you to check if a string of up to 8 words is found in one or more files.
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:
Quote:
Params: C:\*.txt error message

    will report all files txt-files in C:\ that contain the string "error message"


Example:
Quote:
Params: C:\filename.* error message

    will report all files with filename "filename", regardless of extention, in C:\ that contain the string "error message"


If you use the second string search method (see code) you can search strings that are longer than 8 words, you will have to enclose the string in quotation marks now.
Do not forget to comment the first method and uncomment the second before running the script.

Example:
Quote:
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:
@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
:--------------------------------------------------------


Any comment is more than welcome.

Have fun!

Gijsbert van Oeveren
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    KS-Soft Forum Index -> Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group

KS-Soft Forum Index