# Batch Timestamp Comparison with Sys Time



## boredcl (Jun 30, 2010)

I have been working on this for a few days without resolution. Here is a little background. 

I have an application that will run a batch script every 30 min. I need the batch script check for the last modified time of a log file, C:\log.txt. I need to compare this result to the system time. If the log file has not been updated in 30 min compared to the system time, I need the batch file to return a 0. If the log file has been updated less than 30 min compared to the system time, I need the file to return a 1. I also need to do this without creating any temp files if possible. This will run on a Windows Server 2003 box. 

Thanks for your time!


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

This will do the trick I believe, haven't tested it will all regional settings.
This doesn't specifically check the date, so if the log file has not been updated in more than 24 hours you can get an invalid result.
The variable named *_Result* will be set to 0 if it's been more than 30 minutes since the log file was modified, or 1 if it's been updated within the last 30 minutes.
Enter the unquoted path to the file you want to monitor on the 2nd line.

```
@Echo Off
Set _FileName=C:\Temp Dir\log.txt
Call :_GetTime
Set /A _Now=%_ET%
For /F "Tokens=1* Delims= " %%A In ('For %%I In ^("%_FileName%"^) Do @Echo %%~tI') Do Call :_GetTime "%%B"
Set /A _ModTime=%_ET%
If %_Now% LSS %_ModTime% Set /A _Now+=1440
Set /A _Diff=_Now-_ModTime
If %_Diff% LEQ 30 (Set _Result=1) Else Set _Result=0
Echo Logfile was updated %_Diff% minute(s) ago, Result=%_Result%
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::           Subroutines
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_GetTime
If "%~1"=="" (
  For /F "Tokens=*" %%A In ('TIME/T') Do Set _Time=%%A
  ) Else (
  Set _Time=%~1
)
:: Default Delimiter of TAB and Space are used
For /F "Tokens=2*" %%A In ('Reg Query "HKCU\Control Panel\International" /v iTime') Do Set _iTime=%%B
For /F "Tokens=2*" %%A In ('Reg Query "HKCU\Control Panel\International" /v sTime') Do Set _sTime=%%B
If %_iTime%==1 Goto _in24format
For /F "Tokens=1* Delims=%_sTime% " %%A In ('Echo %_Time%') Do (
    Set _Hour=%%A
    Set _Minutes=%%B
)
Set _AMPM=
Echo.%_Minutes%| Find /I "A" >Nul && Set _AMPM=A
Echo.%_Minutes%| Find /I "P" >Nul && Set _AMPM=P
If [%_AMPM%]==[] Set _AMPM=A
Set _Minutes=%_Minutes:~0,2%
If %_Hour:~0,1%==0 Set _Hour=%_Hour:~-1%
If %_Hour% LSS 12 If /I %_AMPM%==P Set /A _Hour=%_Hour%+12
If %_Hour% LSS 10 If /I %_AMPM%==A Set _Hour=0%_Hour%
If %_Hour% EQU 12 If /I %_AMPM%==A Set _Hour=00
Set _Time=%_Hour%%_sTime%%_Minutes%
:_in24format
For /F "Tokens=1,2* Delims=%_sTime% " %%A In ('Echo %_Time%') Do (Set _hh=%%A) & (Set _mm=%%B)
If %_hh:~0,1%==0 Set _hh=%_hh:~-1%
If %_mm:~0,1%==0 Set _mm=%_mm:~-1%
Set /A _ET=%_hh%*60+%_mm%
Goto:EOF
```


----------

