Batch script to detect and check log files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch script to detect and check log files

#16 Post by Aacini » 19 Jan 2015 21:28

The Batch file below use a different approach. It check for missing files inside the currently generated files only:

Code: Select all

@echo off
setlocal

rem Modify the line below if your %DATE% format is not          MM/DD/YYYY
for /F "tokens=1-3 delims=/" %%a in ("%date%") do set "YYYYMMDD=%%c%%a%%b"

set /A count=0, HH=99, MM=145
for %%a in (LOG_%YYYYMMDD%*.txt) do call :checkName %%a
if %count% gtr 0 echo %count% files missing in files generated so far
if %HH%%MM% lss 123145 echo There are files pending to be generated, or LOG_%YYYYMMDD%234500.txt is missing
goto :EOF

:checkName
set "name=%1"
set /A MM+=15
if %MM% equ 160 set /A HH+=1, MM=100
if "%name:~12,4%" equ "%HH:~-2%%MM:~-2%" exit /B
echo LOG_%YYYYMMDD%%HH:~-2%%MM:~-2%00.txt
set /A count+=1
goto checkName

Antonio
Last edited by Aacini on 19 Jan 2015 21:52, edited 2 times in total.

Peter123
Posts: 15
Joined: 19 Jan 2015 03:53

Re: Batch script to detect and check log files

#17 Post by Peter123 » 19 Jan 2015 21:45

@Squashman

Thank you for your reply.

The files are still being generated and accumulated.

Will it be possible to modify the script to take a different date as an input?

I wanted to use this method to check on files created yesterday as it'll have a more complete listing.

Thank you.


- Peter

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: Batch script to detect and check log files

#18 Post by Squashman » 19 Jan 2015 21:48

Now I want you to read this.
viewtopic.php?f=3&t=6108

Peter123
Posts: 15
Joined: 19 Jan 2015 03:53

Re: Batch script to detect and check log files

#19 Post by Peter123 » 19 Jan 2015 21:55

@Aacini

Thank you for your help.

I've tried your script and it's not giving the correct output, see below:

Code: Select all

LOG_2015Tue 2001000000.txt
LOG_2015Tue 2001001500.txt
LOG_2015Tue 2001003000.txt
LOG_2015Tue 2001004500.txt
LOG_2015Tue 2001010000.txt
LOG_2015Tue 2001011500.txt
LOG_2015Tue 2001013000.txt
LOG_2015Tue 2001014500.txt
LOG_2015Tue 2001020000.txt
LOG_2015Tue 2001021500.txt


I've tried to purposely delete some files and the script did not detect it.

Please advice.

Thanks.



- Peter

Peter123
Posts: 15
Joined: 19 Jan 2015 03:53

Re: Batch script to detect and check log files

#20 Post by Peter123 » 19 Jan 2015 21:59

@Squashman

Thanks for your reply and advice.

I'll check and monitor further.

Appreciate for your kind help.


- Peter

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch script to detect and check log files

#21 Post by Aacini » 19 Jan 2015 22:07

@Peter,

It seems that your %date% include the day of the week:

Aacini wrote:

Code: Select all

rem Modify the line below if your %DATE% format is not          MM/DD/YYYY
for /F "tokens=1-3 delims=/" %%a in ("%date%") do set "YYYYMMDD=%%c%%a%%b"



If the output of ECHO %DATE% is "Tue 01/20/2015", then change the "tokens=1-3 delims=/" by "tokens=2-4 delims=/ ". <- Note that there is a space between "/" and the quote!

Antonio

Peter123
Posts: 15
Joined: 19 Jan 2015 03:53

Re: Batch script to detect and check log files

#22 Post by Peter123 » 19 Jan 2015 23:46

@Aacini

Thanks for your reply.

Please see my output below.

C:\Temp>ECHO %DATE%
Tue 01/20/2015



- Peter

Peter123
Posts: 15
Joined: 19 Jan 2015 03:53

Re: Batch script to detect and check log files

#23 Post by Peter123 » 20 Jan 2015 00:11

Subject: Batch script to detect and check log files

Peter123 wrote:@Aacini

I've changed the variable and it seems to be working now.

Here is the output:
LOG_201501010000.txt
1 files missing in files generated so far
There are files pending to be generated, or LOG_201501234500.txt is missed

However, noticed if the actual file format does not end with 00 (zero zero), the script is not able to display the missing file correctly.

For example:
LOG_20150120010001.txt.
-> The file format ended with 01 (zero one).

Script output:
LOG_201501010000.txt

Appreciate if you could help.

Thanks.


- Peter

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch script to detect and check log files

#24 Post by Aacini » 20 Jan 2015 00:51

@Peter,

The program works correctly in my computer:

Code: Select all

C:\> echo %date% %time%
20/01/2015  0:42:14.14

C:\> dir /B
LOG_20150120000001.txt
LOG_20150120001500.txt
LOG_20150120003001.txt
LOG_20150120004500.txt
LOG_20150120010001.txt
test.bat

C:\> test
There are files pending to be generated, or LOG_20150120234500.txt is missing

Please, check where you get the data you posted from. In this example the two "20" characters are missing!

Peter123 wrote:Script output:
LOG_201501010000.txt


Perhaps you modified the program by mistake? Copy the program again and just change this line:

Code: Select all

for /F "tokens=1-3 delims=/" %%a in ("%date%") do set "YYYYMMDD=%%c%%a%%b"

by this one:

Code: Select all

for /F "tokens=2-4 delims=/ " %%a in ("%date%") do set "YYYYMMDD=%%c%%a%%b"


Antonio

Peter123
Posts: 15
Joined: 19 Jan 2015 03:53

Re: Batch script to detect and check log files

#25 Post by Peter123 » 20 Jan 2015 01:04

Hi Antonio,

Thanks for your reply.

I've used the updated comment below and put it into the script and it does not produce anything when there is missing file.

Code: Select all

for /F "tokens=2-4 delims=/ " %%a in ("%date%") do set "YYYYMMDD=%%c%%a%%b"


Here is the complete script after update.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Modify the line below if your %DATE% format is not          MM/DD/YYYY
for /F "tokens=2-4 delims=/ " %%a in ("%date%") do set "YYYYMMDD=%%c%%a%%b"

set /A count=0, HH=99, MM=145
for %%a in (LOG_%YYYYMMDD%*.txt) do call :checkName %%a
if %count% gtr 0 echo %count% files missing in files generated so far
if %HH%%MM% lss 123145 echo There are files pending to be generated, or LOG_%YYYYMMDD%234500.txt is missed
goto :EOF

:checkName
set "name=%1"
set /A MM+=15
if %MM% equ 160 set /A HH+=1, MM=100
if "%name:~12,4%" equ "%HH:~-2%%MM:~-2%" exit /B
echo LOG_%YYYYMMDD%%HH:~-2%%MM:~-2%00.txt
set /A count+=1
goto checkName


Please check and advice if I've missed out anything.

Thanks.


- Peter


Peter123
Posts: 15
Joined: 19 Jan 2015 03:53

Re: Batch script to detect and check log files

#27 Post by Peter123 » 20 Jan 2015 04:00

@foxidrive

Thanks for your help.

I've tried your script and it's working OK, except the same issue with the other script that I've posted earlier.

For example:
I've removed 1 file, see below.
C:\Temp>del LOG_20150120071501.txt

Executed the script and this is the output:
C:\Temp>test01.bat
missing LOG_20150120071500.txt
Press any key to continue . . .


It should be showing file LOG_20150120071501.txt is missing, NOT LOG_20150120071500.txt.

Possible to fix it? Thanks.


- Peter

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: Batch script to detect and check log files

#28 Post by Squashman » 20 Jan 2015 07:22

OMG!!!
The only way to test for the file missing is by date hour and min. How could we possibly know what the second value is of a missing file. You even stated the same thing in your previous posts.

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: Batch script to detect and check log files

#29 Post by Squashman » 20 Jan 2015 08:06

Peter123 wrote:I think the script should do the checking for the filename based on the above format, but exclude checking for the second which should provide a better result since the file is created at 15 minutes interval
- Peter

:roll:

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

Re: Batch script to detect and check log files

#30 Post by foxidrive » 20 Jan 2015 08:15

I deleted my post - I no longer know what the question is meant to be. :D

Post Reply