Page 1 of 1

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

Posted: 04 Jan 2014 22:00
by tbharber
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!

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

Posted: 04 Jan 2014 23:06
by foxidrive
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

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

Posted: 04 Jan 2014 23:16
by tbharber
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?

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

Posted: 04 Jan 2014 23:19
by foxidrive
Yes, write all the temp files to %temp%