[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

Need batch or script to scan text files for certain words

I have daily log files from backups taken each night.  I have reporting enabled to email me if a backup fails midstream or if it cannot find a computer.  The only thing I do not have is a way to notify me if the backup encounters sector errors (or any other errors) during the backup.  The log files will show these types of errors but I am having to open each log file, for over 30 computers, and scan each one.  I would like a batch or script that I can schedule to run each morning, on the backup server, that scans all of the *.log files inside of the log folder and write the results to another text file if it finds the word 'error' any of the log files.  The nice part is that each log file is the name of the computer that was backed up, so maybe this solution could scan the log files, write the output of the .log filename of any that contain the trigger word 'error'.  I could then use blat to have it email me the results text file each morning.
0
murryc
Asked:
murryc
  • 5
  • 5
  • 3
1 Solution
 
Steve KnightIT ConsultancyCommented:
Well finding a file that has "error" in it could be as simple as:

find /n /i *.log "error" > errors.txt
if "%errorlevel%"=="0" call :yourblat
exit

:yourblat
blat.exe .... attaching errors.txt for info.

if the find file finds at least one error in one file then it will attach a file along the lines of :

------- xxxxx.log
------- yyyyy.log
the line with error in it from yyyyyy.log
------- zzzzz.log

etc.

Or we could loop through the available files so that it doesn't include the blank entries for each other computer.... you might like that though anyway as you can see the others were OK?

Steve
0
 
ReneGeCommented:

Hey Steve ;-)

@murryc,
Where are your computer names located, in each lines of your logfile, in the log file name it self?
0
 
ReneGeCommented:
Forget my question, dragon-it's script answers both conditions anyway!!

Cheers,
Rene
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
Steve KnightIT ConsultancyCommented:
If you want to do it more manually then:

@echo off
setlocal enabledelayedexpansion
del all-log.txt 2> NUL
set subject=
for %%a in (*.log) do (
  find /i "error" < "%%a" >> all-log.txt
  if not errorlevel 1 set subject=%%~na !subject!
)

if "%subject%"=="" (
  echo There were no errors
) ELSE (
  echo There were errors with %subject%
  echo Now send all-log.txt as body and use %subject%as subject perhaps?
)
0
 
Steve KnightIT ConsultancyCommented:
Rene, I'm only passing by briefly.... sure you can handle any other bits I've missed on this Steve
0
 
murrycAuthor Commented:
I love your first solution!  Is there a way to have it bail after finding the first 'error' in the log file instead of writing every error line of a single log file to the errors report?  This will make for a much shorter error report when a log file has tons of error lines.
0
 
Steve KnightIT ConsultancyCommented:
Not too easily.  You could use findstr for a different format:

findstr /N /I "error" *.log

You can use /M to only list the filenames.... which is an easy way of getting the error files than in the second example I suppose:

findstr /M "error" *.log

Or you could get a list of the counts of errors on each file with:

find /c /i "error" *.log | find /v ": 0" | find ":"

which would return any error counts for each file then strip out any zeroes, then return the non blank lines

Steve
0
 
murrycAuthor Commented:
I think the original solution works out best after all.  Thanks for the quick support!
0
 
murrycAuthor Commented:
Perfect!
0
 
ReneGeCommented:
Here what I came up so far. I did not test it, so let's see how it does.

 
@ECHO OFF

SET Output=Output.txt
IF EXIST "%Output%" DEL "%Output%"

FOR /F %%A IN ('dir /b *.log') DO CALL :FindError "%%~fA"
EXIT

:GetHeader
FOR /F "delims=" %%A IN ('FINDSTR -i "error" "%~1"') DO (
	ECHO "%~1","%%A">>"%Output%"
	EXIT /b
)

Open in new window

0
 
ReneGeCommented:
Did not realized it was closed. Glad you found your solution.

Cheers,
Rene
0
 
ReneGeCommented:
@ECHO OFF

SET Output=Output.txt
IF EXIST "%Output%" DEL "%Output%"

FOR /F %%A IN ('dir /b *.log') DO CALL :FindError "%%~fA"
EXIT

:FindError
FOR /F "delims=" %%A IN ('FINDSTR -i "error" "%~1"') DO (
	ECHO "%~1","%%A">>"%Output%"
	EXIT /b
)

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
glad it helped.  rene, will look at yours once on pc later... helpful house alarm opposite just woke us up at 5 after bed at 1 :-(
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now