Check C:\windows\tasks\schedlgu.txt Last Run

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Check C:\windows\tasks\schedlgu.txt Last Run

#1 Post by darioit » 02 Dec 2010 05:16

Hello,

in this system file "c:\windows\tasks\schedlgu.txt" there's a log of scheduling task like this

"batch_abc.job" (RUN_batch_abc.bat)
Started 02/12/2010 11.03.00
"batch_abc.job" (RUN_batch_abc.bat)
Finished 02/12/2010 11.04.00
Result: The task completed with an exit code of (0).

Ho can I write a Dos script that check how long time is elapsed beetween last run and now, for check if the time elapsed from last run is greater than a specified time, so to send alert that main batch is not running by hh. hours

Regards
Dario

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#2 Post by aGerman » 02 Dec 2010 18:40

Working with DateTime values is strange using native batch.
Hope this is working for you:

Code: Select all

@echo off

for /f "delims=:" %%a in ('type "C:\Windows\Tasks\SCHEDLGU.TXT"^|findstr /nrc:"\"batch_abc\.job\""') do set /a line=%%a+1
for /f "tokens=2,3" %%a in ('type "C:\Windows\Tasks\SCHEDLGU.TXT"^|findstr /n .^|findstr /bc:"%line%:"') do set "taskDate=%%a" &set "taskTime=%%b"
for /f "tokens=1-6 delims=/." %%a in ("%taskDate%/%taskTime%") do (
  set /a day=1%%a-100
  set /a month=1%%b-100
  set "year=%%c"
  set /a hour=1%%d-100
  set /a minute=1%%e-100
  set /a second=1%%f-100
)

call :calcSec %year% %month% %day% %hour% %minute% %second% sec1


for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v iDate') do set "iDate=%%a"
for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v sDate') do set "sDate=%%a"

for /f "tokens=2" %%i in ("%date%") do set "date=%%i"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%date%") do (
  if %iDate%==0 set /a mm=1%%a-100, dd=1%%b-100, yy=%%c
  if %iDate%==1 set /a dd=1%%a-100, mm=1%%b-100, yy=%%c
  if %iDate%==2 set /a yy=%%a, mm=1%%b-100, dd=1%%c-100
)
if %yy% lss 100 set "yy=20%yy%"

set "hh=%time:~,2%" &set /a nn=1%time:~3,2%-100, ss=1%time:~6,2%-100
if "%hh:~,1%"==" " (set /a hh=%hh%) else set /a hh=1%hh%-100

call :calcSec %yy% %mm% %dd% %hh% %nn% %ss% sec2

set /a delta=(%sec2%-%sec1%)/3600

::if %delta% gtr 12 (
  echo Elapsed time since main batch was running: %delta% hours &pause>nul
::)
goto :eof

:calcSec
setlocal
set /a yy=%1, mm=%2, dd=%3, hh=%4, nn=%5, ss=%6
set /a z=14-mm, z/=12, y=yy+4800-z, m=mm+12*z-3, j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
set /a j=j*86400+hh*3600+nn*60+ss
endlocal &set /a %7=%j%
goto :eof


Regards
aGerman

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#3 Post by orange_batch » 02 Dec 2010 19:41

:shock:

*uses defib on aGerman*

Damn it, darioit! You killed him!

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#4 Post by darioit » 03 Dec 2010 02:53

Hello aGerman,

Thanks for your help, but doesn't work because variable sec1 is not set pherhaps something is missing

Regards
Dario

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#5 Post by darioit » 03 Dec 2010 03:06

I find the problem

in the middle of log schedlgu.txt I have other batch running, perhaps this disturbing batch

"batch_abc.job" (RUN_batch_abc.bat)
Started 02/12/2010 11.03.00
"another_batch.job" (another_batch_001.bat)
Started 02/12/2010 11.05.00
"batch_abc.job" (RUN_batch_abc.bat)
Finished 02/12/2010 11.10.00
Result: The task completed with an exit code of (0).

Regards
Dario

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#6 Post by aGerman » 03 Dec 2010 11:32

@orange_batch
Don't worry, I'm still alive :wink:

@darioit
You should check the variables in the first part of the batch (by ECHOing them).
%line% - The line number after the last line where "batch_abc.job" was found.
%taskDate% and %taskTime% - Date and time as shown in the line.
%day% ... %second% - The single tokens of date and time without leading zeros.

Please come back with the information which one fails first.

Regards
aGerman

[EDIT]
I checked my own schedlgu.txt and noticed that there is a tab character before Started and Finished. In this case you have to change
for /f "tokens=2,3" %%a in ('type ...
to
for /f "tokens=3,4" %%a in ('type ...
in the 2nd line.
[/EDIT]

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#7 Post by darioit » 04 Dec 2010 01:35

Hello aGerman,

I find a problem, this in not correct log file
"batch_abc.job" (RUN_batch_abc.bat)
Started 02/12/2010 11.03.00

but this
"batch_abc.job" (RUN_batch_abc.bat)
Started 02/12/2010 11.03.00

Started and Finish is preceded by a TAB and is not at beginning of the line

I try to remove /b in findstr but doesn't work

Regards
Dario

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#8 Post by aGerman » 04 Dec 2010 07:53

darioit wrote:I find a problem, this in not correct log file
"batch_abc.job" (RUN_batch_abc.bat)
Started 02/12/2010 11.03.00

but this
"batch_abc.job" (RUN_batch_abc.bat)
Started 02/12/2010 11.03.00

LOL
As you can see it looks exactly the same in your post. Use code tags next time.


darioit wrote:Started and Finish is preceded by a TAB and is not at beginning of the line

Have a look at the EDIT in my previous posting. This may help.


darioit wrote:I try to remove /b in findstr but doesn't work

No. The /b switch is only to find the line number (generated by /n) on the beginning of the line. You need it to distinguish e.g. 12: and 112: if you are looking for line 12.
Why didn't you follow my suggestion? Display the values of the variables to find out what happens.

Regards
aGerman

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#9 Post by darioit » 04 Dec 2010 09:35

Lol, it is true as my mother, my wife and my boss always says... you do not hear what I say.

You was right I fix the 2 line changing "tokens=3,4" and voilà, the script running well!

Thank you for everything

Regards
Dario

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#10 Post by darioit » 05 Dec 2010 16:53

Hello,

this is a real matter, the last line of log is not the last event, but the last event is before the line
[ ***** La voce più recente si trova al di sopra di questa riga ***** ]


This is a part of my log:

Code: Select all

[quote]"Scheduled Update for Ask Toolbar.job" (UpdateTask.exe)
   Terminata 05/12/2010 23.01.01
   Esito: Operazione completata con un codice di uscita (0).
[ ***** La voce più recente si trova al di sopra di questa riga ***** ]

                                       
"Scheduled Update for Ask Toolbar.job" (UpdateTask.exe)
   Terminata 03/12/2010 7.01.00
   Esito: Operazione completata con un codice di uscita (0).[/quote]
[list=][/list]
How can solve this problem?

Regards
Dario

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#11 Post by aGerman » 06 Dec 2010 12:45

Um, please ask M$ never providing those §#©$!¿& things :evil:
The only way that I see is reading each pertinent line, calculate the seconds and compare the value, what is the latest. Probably run time of some minutes if you have some thousand lines in your file. Better you check this manually.

Regards
aGerman

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#12 Post by aGerman » 06 Dec 2010 15:46

Well Dario, because I was kinda bored I took a cold shower and tried to solve this problem.
This works for your example:

Code: Select all

@echo off &setlocal enabledelayedexpansion

set /a sec1=0
for /f "tokens=1* delims=:" %%a in ('type "C:\Windows\Tasks\SCHEDLGU.TXT"^|findstr /n .') do (
  ECHO Line in process: %%a
  echo\%%b|findstr /rc:"\"Scheduled Update for Ask Toolbar\.job\"">nul &&set /a line=%%a+1
  if "!line!"=="%%a" (
    for /f "tokens=2,3" %%c in ("%%b") do (
      for /f "tokens=1-6 delims=/." %%e in ("%%c/%%d") do (
        set /a day=100%%e %% 100, month=100%%f %% 100, hour=100%%h %% 100, minute=100%%i %% 100, second=100%%j %% 100
        set "year=%%g"
      )
    )
    set /a sec1_before=sec1
    call :calcSec !year! !month! !day! !hour! !minute! !second! sec1
    if !sec1_before! gtr !sec1! set /a sec1=sec1_before
  )
)


for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v iDate') do set "iDate=%%a"
for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v sDate') do set "sDate=%%a"

for /f "tokens=2" %%i in ("%date%") do set "date=%%i"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%date%") do (
  if %iDate%==0 (set /a mm=100%%a %% 100, dd=100%%b %% 100 &set "yy=%%c")
  if %iDate%==1 (set /a dd=100%%a %% 100, mm=100%%b %% 100 &set "yy=%%c")
  if %iDate%==2 (set "yy=%%a" &set /a mm=100%%b %% 100, dd=100%%a %% 100)
)
if %yy% lss 100 set "yy=20%yy%"

set "hh=%time:~,2%" &set /a nn=1%time:~3,2%-100, ss=1%time:~6,2%-100
if "%hh:~,1%"==" " (set /a hh=%hh%) else set /a hh=1%hh%-100

call :calcSec %yy% %mm% %dd% %hh% %nn% %ss% sec2

set /a delta=(%sec2%-%sec1%)/3600

::if %delta% gtr 12 (
  cls&echo Elapsed time since main batch was running: %delta% hours &pause>nul
::)
goto :eof

:calcSec
setlocal
set /a z=14-%2, z/=12, y=%1+4800-z, m=%2+12*z-3, j=153*m+2
set /a j=j/5+%3+y*365+y/4-y/100+y/400-2472633
set /a j=j*86400+%4*3600+%5*60+%6
endlocal &set /a %7=%j%
goto :eof

Regards
aGerman

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#13 Post by darioit » 06 Dec 2010 17:22

Thanks aGerman,

what about my variation at the beginning of code, is good enough?

Code: Select all

@echo off
setlocal enabledelayedexpansion

FOR /F "delims=" %%a in (C:\Windows\Tasks\SCHEDLGU.TXT) do set "name=%%a" &call :procName %1

:procName
IF "%name:~2,3%"=="***" (goto :Main
) else (
>> D:\TEMP.TXT ECHO !name!
)
Goto :eof

:Main
for /f "delims=:" %%a in ('type "D:\TEMP.TXT"^|findstr /nrc:"\"Scheduled Update for Ask Toolbar\.job\""') do set /a line=%%a+1
for /f "tokens=3,4" %%a in ('type "D:\TEMP.TXT"^|findstr /n .^|findstr /bc:"%line%:"') do set "taskDate=%%a" &set "taskTime=%%b"


Regards
Dario

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#14 Post by aGerman » 06 Dec 2010 18:28

I didn't try your code, because it's time for me to hit the sack.

It seems to me that it will not work. You're looking for the asterisks and if you find them you run the code. But:
-What happens if there are no asterisks in the file (because it's an exception - I checked it whith my schedlgu.txt)?
-What happens next week if maybe a lot of further lines are written to the file? Would you find the latest entry this way?
-Are you realy sure you will find the asterisks only for this task? What if this line doesn't belong to the "Scheduled Update for Ask Toolbar.job"?

Regards
aGerman

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Check C:\windows\tasks\schedlgu.txt Last Run

#15 Post by darioit » 07 Dec 2010 04:19

OK you're right, this is my final code. Post close. Many Thanks. Regards. Dario

Code: Select all

@echo off &setlocal enabledelayedexpansion

set JOBS=%1
set /a sec1=0
for /f "tokens=1* delims=:" %%a in ('type "C:\Windows\Tasks\SCHEDLGU.TXT"^|findstr /n .') do (
 rem  ECHO Line in process: %%a
  echo\%%b|findstr /rc:"\"%JOBS%\.job\"">nul &&set /a line=%%a+1
  if "!line!"=="%%a" (
    for /f "tokens=2,3" %%c in ("%%b") do (
      for /f "tokens=1-6 delims=/." %%e in ("%%c/%%d") do (
        set /a day=100%%e %% 100, month=100%%f %% 100, hour=100%%h %% 100, minute=100%%i %% 100, second=100%%j %% 100
        set "year=%%g"
      )
    )
    set /a sec1_before=sec1
    call :calcSec !year! !month! !day! !hour! !minute! !second! sec1
    if !sec1_before! gtr !sec1! set /a sec1=sec1_before
  )
)

for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v iDate') do set "iDate=%%a"
for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v sDate') do set "sDate=%%a"

for /f "tokens=2" %%i in ("%date%") do set "date=%%i"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%date%") do (
  if %iDate%==0 (set /a mm=100%%a %% 100, dd=100%%b %% 100 &set "yy=%%c")
  if %iDate%==1 (set /a dd=100%%a %% 100, mm=100%%b %% 100 &set "yy=%%c")
  if %iDate%==2 (set "yy=%%a" &set /a mm=100%%b %% 100, dd=100%%a %% 100)
)
if %yy% lss 100 set "yy=20%yy%"

set "hh=%time:~,2%" &set /a nn=1%time:~3,2%-100, ss=1%time:~6,2%-100
if "%hh:~,1%"==" " (set /a hh=%hh%) else set /a hh=1%hh%-100

call :calcSec %yy% %mm% %dd% %hh% %nn% %ss% sec2

SET /a TookTime=(%sec2%-%sec1%)*100
SET /a delta=(%sec2%-%sec1%)/3600
SET /a MINS=%TookTime:~0,-2%/60
SET /a SECS=%TookTime:~0,-2%-%MINS%*60
SET ELAPSED=%delta% Ore %MINS% Minuti %SECS% Secondi
SET /a TookTimeSec = %TookTime%/60


:: if %delta% gtr 0 (
  echo %date% %time% Elapsed Time From Last Batch Run "%JOBS%" %Elapsed%
:: )
goto :eof

:calcSec
setlocal
set /a z=14-%2, z/=12, y=%1+4800-z, m=%2+12*z-3, j=153*m+2
set /a j=j/5+%3+y*365+y/4-y/100+y/400-2472633
set /a j=j*86400+%4*3600+%5*60+%6
endlocal &set /a %7=%j%
goto :eof

Post Reply