0

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
)
DextrousDave
  • 6,603
  • 35
  • 91
  • 134
  • An appropriate sample of log.txt from each machine would be useful, as would a sample of `%%~ta`. You've stated YY-MM-DD format, yet just once for no apparent reason switch to YY-DD-MM in your narrative. Is it possible that your problem arises when the last 'DATABASE IS OK` line in the log file occurs before midnight but the logfile has been updated after midnight and hence has a different date? – Magoo Aug 01 '13 at 03:48

1 Answers1

2

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
)
RGuggisberg
  • 4,630
  • 2
  • 18
  • 27
  • thank you for your answer.Not sure how to implement this practically? I did not write the script myself – DextrousDave Jul 31 '13 at 15:20
  • thank you very much RGuggisberg, really appreciate your answer. It works on my Win7 PC, have not tested on the server yet, since the server is at my work, so I will test it tomorrow. As I said, on my server at work, the datetime format of windows, and hence the log file, is YYYY-MM-DD and the date entries in the file is YYY/MM/DD. Will this be an issue? Otherwise, we can always run a command that first changes all the date entries in the text file to the same time format as that of the server, which is: YYY-MM-DD – DextrousDave Jul 31 '13 at 18:03
  • Not sure if you typed the right number of Ys in your comment YYYY or YYY? Might be easier to replace characters in success between the 2 FOR loops with set success=%success:-=/% or you can replace the characters in filedate just prior to the IF statement set !filedate:/-=! *** untested *** – RGuggisberg Jul 31 '13 at 18:30
  • yup I meant YYYY...yeah that might not be a bad idea...will try this all out tomorrow and let you know... – DextrousDave Jul 31 '13 at 19:55
  • yor last comment - "or you can replace the characters in filedate just prior to the IF statement set !filedate:/-=! *** untested *** " - What Characters are you talking about exactly? – DextrousDave Jul 31 '13 at 20:16
  • Take a look at set /? You can either change / to - or - to / whatever makes more sense to you. set !filedate:/-=! would change all occurrences of / to - in filedate. – RGuggisberg Jul 31 '13 at 20:45
  • Hi RGuggisberg - I have just tested it and it fails again, BUT we know why, see the line I removed that gives the output for what is being compared...see update you my question... – DextrousDave Aug 01 '13 at 10:41
  • Date formats vary by region. Check your pc settings. Sounds like you may have it figured out. – RGuggisberg Aug 01 '13 at 10:54
  • Hey RGuggisberg. OI have tried everything you said and I just can't figure it out. Look at my "update2" to see what I have added but it does not work... – DextrousDave Aug 01 '13 at 13:55
  • Ok... there was some confusion over which format had the dashes and which one had the slashes (we had them reversed). Now that you have posted the details we can see what is going on... so use this line (the ^ is to escape the slash). set %success:^/=-% – RGuggisberg Aug 01 '13 at 16:29
  • I updated my last update (Update 2) with the last code - set %success:^/=-% -But when I echo the value of success it still gives the date value with slashes. Did I use it incorrectly? – DextrousDave Aug 01 '13 at 17:09
  • Hallo again. I got it to work, but not by using your code alone. I first use REPL.BAT (http://stackoverflow.com/a/16735079/1012053) to change all the date entries in the log file to format with dashes: YYYY-MM-DD. So then your awesome script runs, matches the date of the latest logfile entry(whicg was changed of format by REPL.BAT) WITH the date stam on the logfile, which is also in YYYY-MM-DD format. et voila, it works. BUT, if we can fix it in your file, then we do not need to use the REPL.BAT file and we have a simpler and more efficient solution... – DextrousDave Aug 01 '13 at 18:12
  • You are correct. We can get there w/o REPL.bat. I edited my code above. It should have been set success=%success:/=-% – RGuggisberg Aug 01 '13 at 18:20
  • Eureka! Thanks man, that works a charm...! Really appreciate your ongoing effort and time. You Rock – DextrousDave Aug 01 '13 at 18:22