DextrousDave
DextrousDave

Reputation: 6793

Batch File Date Format not right

I have the following code that checks a log file for a specific string, and based on a datestamp matching it executes certain tasks.

Now this code below works great on a windows 7 machine with the Date-Time format of: YY-MM-DD hh:mm, but executing the exact same batch file on Windows Server 2008 with date-time format of: YY-MM-DD hh:mm it does not work - I suspect it might be the date-time format... Could anyone confirm if the date-time format used in the batch file will work for the YY-DD-MM date format?

Also, what if the date time format in the log file differs from the dat-time format of the log file itself? Will the code still work?

for /f "tokens=2" %%a in ('findstr /i /c:"DATABASE IS OK" log.txt') do set "success=%%a"
for %%a in (log.txt) do set "filedate=%%~ta"
if "%filedate:~0,10%"=="%success%" (
    call another.bat
) else (
    >>otherlogfile.log echo(%date% %time% DATABASE UNSUCCESSFUL
)

Thank you


Update 1:

C:\Utilities\Filter>for %a in (logfile.txt) do set "filedate=%~ta"

C:\Utilities\Filter>set "filedate=2013-07-31 21:31"

C:\Utilities\Filter>REM If it still does not work remove REM from next
 line so we can see what is being compared

C:\Utilities\Filter>ECHO.filedate=!filedate:~0,10!]   success=2013/07/
31]
filedate=2013-07-31]   success=2013/07/31]

C:\Utilities\Filter>pause
Press any key to continue . . .

As you can see, the dates beign compared will never match, since the format is not correct.

filedate=2013-07-31]   success=2013/07/31]

What do you suggest?


Update 2:

setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('findstr /i /c:"DATABASE IS OK" logfile.txt') do set "success=%%a"

set "%success:^/=-%"
echo %success%
pause
for %%a in (logfile.txt) do set "filedate=%%~ta"

REM If it still does not work remove REM from next line so we can see what is being compared
ECHO.filedate=!filedate:~0,10!]   success=%success%]

pause
if "!filedate:~0,10!"=="%success%" (
    call another.bat
) else (
    >>readlogFail.txt echo(%date% %time% DATABASE UNSUCCESSFUL
)

Upvotes: 0

Views: 1436

Answers (1)

RGuggisberg
RGuggisberg

Reputation: 4750

The first thing I notice is that if you want to interrogate the run time value of filedate in the FOR loop you need to SETLOCAL ENABLEDELAYEDEXPANSION and then use ! instead of %. Most likely it just happens to work on Windows 7 because the load time value of success and filedate are the same. Try the code below. You may find that there are other things wrong... but let's do this first. Note that success is not interrogated within a FOR/IF construct, but filedate is.

setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('findstr /i /c:"DATABASE IS OK" log.txt') do set "success=%%a"
set success=%success:/=-%
for %%a in (log.txt) do set "filedate=%%~ta"
REM If it still does not work remove REM from next line so we can see what is being compared
ECHO.filedate=!filedate:~0,10!]   success=%success%]
if "!filedate:~0,10!"=="%success%" (
    call another.bat
) else (
    >>otherlogfile.log echo(%date% %time% DATABASE UNSUCCESSFUL
)

Upvotes: 2

Related Questions