I want to create a batch file that executes a command, but before the command is executed, it must look up a log to see if it contains the following line:

DOFWADX 2013/07/24 15:42:33 -I- DATABASE IS OK.

I am looking for the specific text - "DATABASE IS OK". The problem is that there will be multiple entries of this line in the log, with the latest ones being appended to the file.

My Question: How do I look for the latest/last entry in the log file containing "DATABASE IS OK". Also, is there a way we can get the time the log file was changed/last updated and filter the "DATABASE IS OK" line by that date and time entry, if it exists in the log file.


DOFWADX [date and time log file was changed/updated] -I- [look for this line >>]DATABASE IS OK.

Once this line with the latest datestamp is found, I want to execute a command, doesn't matter what it is now.

  • So will your script choose to NOT execute the "command" if the latest datestamp (of the found line) hasn't changed since the last time the script checked? – dtmland Jul 24 '13 at 18:44
  • yes, that is right – DextrousDave Jul 24 '13 at 19:33

The file update timestamp won't help you. You need to store the time of your last check (i.e. running the batch) instead. The following batch stores the last check time in a file:

@echo off
setlocal enabledelayedexpansion

:: Check parameters.
if "%~1" == "" (
    echo Usage: readlog.bat example.log
    exit /B 1

:: Read time of last check from example.log.lastcheck file, or create one.
if exist "%~1.lastcheck" (
    for /F "usebackq tokens=*" %%L in ("%~1.lastcheck") do set lastcheck=%%L
) else (
    set lastcheck=0

:: Update example.log.lastcheck file with current time.
call :getunixtime %date:~-4% %date:~-8,2% %date:~-12,2% %time:~,2% %time:~3,2% %time:~6,2%
echo %unixtime% > "%~1.lastcheck"

:: Process log.
for /F "usebackq tokens=2-7 delims=/: " %%A in (`type test.log ^| findstr /c:"DATABASE IS OK"`) do (
    call :getunixtime %%A %%B %%C %%D %%E %%F

    :: if an OK entry older than last check exist
    if !unixtime! GTR %lastcheck% (
        echo doing my stuff
        goto :done

exit /B 0

:: Poor man's implementation of Unix time.
set "unixtime=(%1-1970)*365*24*3600 + %2*30*24*3600 + %3*24*3600 + %4*3600 + %5*60 + %6"
:: Remove leading zeros to prevent octal interpretation.
set "unixtime=%unixtime: 0= %"
set /a "unixtime=%unixtime%"
goto :eof
  • Thank you for your answer. I am trying to implement this - Would Unixtime commands work within windows...This is for Windows Commandline – DextrousDave Jul 30 '13 at 9:40
  • @DextrousDave The whole batch is for Windows. The getunixtime function is just poor man's (or naive if you want) implementation of Unix time (which is number of seconds since Jan 1 1970). Yet still perfectly suitable for your needs. – Vlastimil Ovčáčík Jul 31 '13 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.