file update time compared to system time

June 30, 2010 at 13:41:12
Specs: Windows Server 2003
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!


See More: file update time compared to system time

Report •

#1
June 30, 2010 at 13:42:50
Your best bet would be to pick a language with date support, like VBScript.

Report •

#2
June 30, 2010 at 15:06:58
Just something I threw together real quick...
Good through 2060?!

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET standard_year=312831303130313130313031
SET     leap_year=312931303130313130313031

FOR /F "delims=" %%z IN ('DIR /B /S "C:\log.txt"') DO (
    CALL :File_DateTime "%%z"
    CALL :Current_DateTime %DATE% %TIME%
    SET /A minutes_different=!total_current_minutes!-!total_file_minutes!
    SET /A years_different=!minutes_different!/525600
    SET /A months_different=!minutes_different!/43800
    SET /A days_different=!minutes_different!/1440
    ECHO %%z was last modified !minutes_different! minutes ago...
    IF "!minutes_different!" LSS "30" (
        SET EXIT_RC=1
    ) ELSE (
        SET EXIT_RC=0
    )
)
EXIT /B !EXIT_RC!

:File_DateTime
SET FileDateTime=%~t1
FOR /F "tokens=1-6 delims=/: " %%a IN ("!FileDateTime!") DO (
    SET file_month=%%a
    SET file_date=%%b
    SET file_year=%%c
    SET file_hour=%%d
    SET file_minute=%%e
    SET file_period=%%f
    :Raw variables are set. Format them to allow math.
    SET file_minute=#!file_minute!
    SET file_minute=!file_minute:#0=!
    SET file_minute=!file_minute:#=!
    SET file_hour=#!file_hour!
    SET file_hour=!file_hour:#0=!
    SET file_hour=!file_hour:#=!
    SET file_date=#!file_date!
    SET file_date=!file_date:#0=!
    SET file_date=!file_date:#=!
    SET file_month=#!file_month!
    SET file_month=!file_month:#0=!
    SET file_month=!file_month:#=!
    :Variables formatted. Perform math on them:
    IF "!file_period!" EQU "PM" (
        IF NOT !file_hour! EQU 12 (
            SET /A file_hour=!file_hour!+12
        )
    )
    CALL :Is_leap_year? !file_year!
    SET /A total_file_minutes=!file_minute!
    SET /A total_file_minutes=!total_file_minutes!+!file_hour!*60
    SET /A total_file_minutes=!total_file_minutes!+!file_date!*1440
    SET /A prior_month=!file_month!-1
    FOR /L %%g IN (1,1,!prior_month!) DO (
        SET /A pos=%%g-1
        SET /A pos=!pos!*2
        FOR /L %%h IN (!pos!,1,!pos!) DO (
            SET month_days=!which_year:~%%h,2!
            SET /A total_file_minutes=!total_file_minutes!+!month_days!*1440
        )
    )
    SET /A prior_year=!file_year!-1
    CALL :!prior_year!
    SET /A total_file_minutes=!total_file_minutes!+!total_minutes!
)
GOTO :EOF 

:Current_DateTime
SET CurrentDateTime=%*
FOR /F "tokens=1-6 delims=/: " %%a IN ("!CurrentDateTime!") DO (
    SET current_weekday=%%a
    SET current_month=%%b
    SET current_date=%%c
    SET current_year=%%d
    SET current_hour=%%e
    SET current_minute=%%f
    :Raw variables are set. Format them to allow math:
    SET current_date=#!current_date!
    SET current_date=!current_date:#0=!
    SET current_date=!current_date:#=!
    SET current_month=#!current_month!
    SET current_month=!current_month:#0=!
    SET current_month=!current_month:#=!
    :Variables formatted. Perform math on them:
    CALL :Is_leap_year? !current_year!
    SET /A total_current_minutes=!current_minute!
    SET /A total_current_minutes=!total_current_minutes!+!current_hour!*60
    SET /A total_current_minutes=!total_current_minutes!+!current_date!*1440
    SET /A prior_month=!current_month!-1
    FOR /L %%g IN (1,1,!prior_month!) DO (
        SET /A pos=%%g-1
        SET /A pos=!pos!*2
        FOR /L %%h IN (!pos!,1,!pos!) DO (
            SET month_days=!which_year:~%%h,2!
            SET /A total_current_minutes=!total_current_minutes!+!month_days!*1440
        )
    )
    SET /A prior_year=!current_year!-1
    CALL :!prior_year!
    SET /A total_current_minutes=!total_current_minutes!+!total_minutes!
)
GOTO :EOF 

:Is_leap_year?
SET year=%1
SET /A test1=%year%%%4
IF !test1! EQU 0 (
    SET /A test2=%year%%%100
    IF !test2! EQU 0 (
        SET /A test3=%year%%%400
        IF !test3! EQU 0 (
            SET which_year=%leap_year%
        ) ELSE (
            SET which_year=%standard_year%
        ) 
    ) ELSE (
        SET which_year=%leap_year%
    )
) ELSE (
    SET which_year=%standard_year%
)
GOTO :EOF

:1960
SET total_minutes=1030860000
GOTO :EOF
:1961
SET total_minutes=1031385600
GOTO :EOF
:1962
SET total_minutes=1031911200
GOTO :EOF
:1963
SET total_minutes=1032436800
GOTO :EOF
:1964
SET total_minutes=1032963840
GOTO :EOF
:1965
SET total_minutes=1033489440
GOTO :EOF
:1966
SET total_minutes=1034015040
GOTO :EOF
:1967
SET total_minutes=1034540640
GOTO :EOF
:1968
SET total_minutes=1035067680
GOTO :EOF
:1969
SET total_minutes=1035593280
GOTO :EOF
:1970
SET total_minutes=1036118880
GOTO :EOF
:1971
SET total_minutes=1036644480
GOTO :EOF
:1972
SET total_minutes=1037171520
GOTO :EOF
:1973
SET total_minutes=1037697120
GOTO :EOF
:1974
SET total_minutes=1038222720
GOTO :EOF
:1975
SET total_minutes=1038748320
GOTO :EOF
:1976
SET total_minutes=1039275360
GOTO :EOF
:1977
SET total_minutes=1039800960
GOTO :EOF
:1978
SET total_minutes=1040326560
GOTO :EOF
:1979
SET total_minutes=1040852160
GOTO :EOF
:1980
SET total_minutes=1041379200
GOTO :EOF
:1981
SET total_minutes=1041904800
GOTO :EOF
:1982
SET total_minutes=1042430400
GOTO :EOF
:1983
SET total_minutes=1042956000
GOTO :EOF
:1984
SET total_minutes=1043483040
GOTO :EOF
:1985
SET total_minutes=1044008640
GOTO :EOF
:1986
SET total_minutes=1044534240
GOTO :EOF
:1987
SET total_minutes=1045059840
GOTO :EOF
:1988
SET total_minutes=1045586880
GOTO :EOF
:1989
SET total_minutes=1046112480
GOTO :EOF
:1990
SET total_minutes=1046638080
GOTO :EOF
:1991
SET total_minutes=1047163680
GOTO :EOF
:1992
SET total_minutes=1047690720
GOTO :EOF
:1993
SET total_minutes=1048216320
GOTO :EOF
:1994
SET total_minutes=1048741920
GOTO :EOF
:1995
SET total_minutes=1049267520
GOTO :EOF
:1996
SET total_minutes=1049794560
GOTO :EOF
:1997
SET total_minutes=1050320160
GOTO :EOF
:1998
SET total_minutes=1050845760
GOTO :EOF
:1999
SET total_minutes=1051371360
GOTO :EOF
:2000
SET total_minutes=1051898400
GOTO :EOF
:2001
SET total_minutes=1052424000
GOTO :EOF
:2002
SET total_minutes=1052949600
GOTO :EOF
:2003
SET total_minutes=1053475200
GOTO :EOF
:2004
SET total_minutes=1054002240
GOTO :EOF
:2005
SET total_minutes=1054527840
GOTO :EOF
:2006
SET total_minutes=1055053440
GOTO :EOF
:2007
SET total_minutes=1055579040
GOTO :EOF
:2008
SET total_minutes=1056106080
GOTO :EOF
:2009
SET total_minutes=1056631680
GOTO :EOF
:2010
SET total_minutes=1057157280
GOTO :EOF
:2011
SET total_minutes=1057682880
GOTO :EOF
:2012
SET total_minutes=1058209920
GOTO :EOF
:2013
SET total_minutes=1058735520
GOTO :EOF
:2014
SET total_minutes=1059261120
GOTO :EOF
:2015
SET total_minutes=1059786720
GOTO :EOF
:2016
SET total_minutes=1060313760
GOTO :EOF
:2017
SET total_minutes=1060839360
GOTO :EOF
:2018
SET total_minutes=1061364960
GOTO :EOF
:2019
SET total_minutes=1061890560
GOTO :EOF
:2020
SET total_minutes=1062417600
GOTO :EOF
:2021
SET total_minutes=1062943200
GOTO :EOF
:2022
SET total_minutes=1063468800
GOTO :EOF
:2023
SET total_minutes=1063994400
GOTO :EOF
:2024
SET total_minutes=1064521440
GOTO :EOF
:2025
SET total_minutes=1065047040
GOTO :EOF
:2026
SET total_minutes=1065572640
GOTO :EOF
:2027
SET total_minutes=1066098240
GOTO :EOF
:2028
SET total_minutes=1066625280
GOTO :EOF
:2029
SET total_minutes=1067150880
GOTO :EOF
:2030
SET total_minutes=1067676480
GOTO :EOF
:2031
SET total_minutes=1068202080
GOTO :EOF
:2032
SET total_minutes=1068729120
GOTO :EOF
:2033
SET total_minutes=1069254720
GOTO :EOF
:2034
SET total_minutes=1069780320
GOTO :EOF
:2035
SET total_minutes=1070305920
GOTO :EOF
:2036
SET total_minutes=1070832960
GOTO :EOF
:2037
SET total_minutes=1071358560
GOTO :EOF
:2038
SET total_minutes=1071884160
GOTO :EOF
:2039
SET total_minutes=1072409760
GOTO :EOF
:2040
SET total_minutes=1072936800
GOTO :EOF
:2041
SET total_minutes=1073462400
GOTO :EOF
:2042
SET total_minutes=1073988000
GOTO :EOF
:2043
SET total_minutes=1074513600
GOTO :EOF
:2044
SET total_minutes=1075040640
GOTO :EOF
:2045
SET total_minutes=1075566240
GOTO :EOF
:2046
SET total_minutes=1076091840
GOTO :EOF
:2047
SET total_minutes=1076617440
GOTO :EOF
:2048
SET total_minutes=1077144480
GOTO :EOF
:2049
SET total_minutes=1077670080
GOTO :EOF
:2050
SET total_minutes=1078195680
GOTO :EOF
:2051
SET total_minutes=1078721280
GOTO :EOF
:2052
SET total_minutes=1079248320
GOTO :EOF
:2053
SET total_minutes=1079773920
GOTO :EOF
:2054
SET total_minutes=1080299520
GOTO :EOF
:2055
SET total_minutes=1080825120
GOTO :EOF
:2056
SET total_minutes=1081352160
GOTO :EOF
:2057
SET total_minutes=1081877760
GOTO :EOF
:2058
SET total_minutes=1082403360
GOTO :EOF
:2059
SET total_minutes=1082928960
GOTO :EOF
:2060
SET total_minutes=1083456000
GOTO :EOF

Insomniac at large


Report •
Related Solutions


Ask Question