Find all .txt & .log files modified today and display errors

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tbharber
Posts: 32
Joined: 17 Sep 2010 17:08

Find all .txt & .log files modified today and display errors

#1 Post by tbharber » 04 Jan 2014 22:00

Hey All,

I need to add a few lines to a script I am working on that will look at all .txt and .log files in a folder that were modified today and display all lines in the files that contain the text | ERROR |. For example it would look like the following....

File1.txt:
04/01/14 20:28:02.663 | ERROR | 29 | Error123456...
04/01/14 21:28:02.748 | ERROR | 29 | ErrorXYZ...

File2.log:
04/01/14 22:28:02.849 | ERROR | 29 | Errorzyx....
04/01/14 16:52:11.935 | ERROR | 91 | ErrorABC....
04/01/14 09:52:01.191 | ERROR | 3 | Error1234....


I can do this manually one by one to each file, but do not know how to have it do this automatically for all files modified today. Any help would be amazing!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find all .txt & .log files modified today and display er

#2 Post by foxidrive » 04 Jan 2014 23:06

This works here - but the lines in the log file need to have CRLF line ends.

Change d:\folder to the path you need. It creates result.txt in the same folder - see if that is sufficient for your needs.

Code: Select all

@echo off
pushd "d:\folder"
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%"
 del file2.tmp 2>nul
 del report.txt 2>nul
 for /f "delims=" %%a in ('dir *.txt *.log /b /a-d ') do call :getdate "%%~fa"
 if exist file2.tmp (
   findstr /f:file2.tmp /L /c:"| ERROR |"  >report.txt
   ) else ( echo no files were modified today)
del file2.tmp 2>nul
popd
pause
goto :EOF
 :getdate
 set "file=%~1"
 set "file=%file:\=\\%"
 WMIC DATAFILE WHERE name="%file%" get lastmodified | find "." >file.tmp
 for /f %%b in (file.tmp) do set dt=%%b
 if %datestamp% EQU %dt:~0,8% for /f "delims=" %%b in ("%~1") do >>file2.tmp echo %%b
 del file.tmp

tbharber
Posts: 32
Joined: 17 Sep 2010 17:08

Re: Find all .txt & .log files modified today and display er

#3 Post by tbharber » 04 Jan 2014 23:16

This looks cool, the only problem is that the server I am doing this to I only have read access to. Can you think of a way to do it where we are not creating/deleting temp files?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find all .txt & .log files modified today and display er

#4 Post by foxidrive » 04 Jan 2014 23:19

Yes, write all the temp files to %temp%

Post Reply