1.

Solve : Comparing time as a string?

Answer»

Hello Experts,

I'm tring to check if a file arrives before midnight. However the "if time equals" does not work. Here is the relavant section, any suggestions? Thanks in advance!

:BEGINWAIT
FOR /F "TOKENS=*" %%A IN ('TIME/T') DO SET TIME=%%A
IF EXIST %PathInbox%\NewFile.DAT GOTO FILEEXIST
IF "%TIME%" == "12:00 AM" GOTO ENDTIMEWAIT

GOTO BEGINWAITYou are going to hit a problem of name collision because %TIME% is already a system variable, (guess what it contains?). Choose a DIFFERENT name eg %mytime%. That should do it.

On my system, right now, %time% reads 20:40:07.47

(I am a euro weenie who uses the 24 hr clock, so time /t produces 20:40)


If you STILL need help I'd need to know between what times you'd want it to check. Because it's ALWAYS before Midnight.Quote from: DeltaSlaya on August 28, 2007, 03:12:11 PM

If you still need help I'd need to know between what times you'd want it to check. Because it's always before Midnight.

Good point Delta. That code must be started manually every day I guess.

Well if you're starting it manually it doesn't even need to check the time then, just the existence of the file.Quote from: DeltaSlaya on August 28, 2007, 03:36:21 PM
Well if you're starting it manually it doesn't even need to check the time then, just the existence of the file.

Yes it does, it has to wait around until the next midnight. Did you read the batch code at all?
Oh, I see, so it's SUPPOSED to loop until the file arrives or the time is midnight? Thats easy enough.if that's the case, it would be simpler to just schedule a job at or after 12 midnight to query the existence of the file, instead of looping everytime.As it stands the code decides whether midnight has arrived by looking for "time /t" giving "12:00 AM" as an output. So strictly speaking it is not checking whether midnight has passed, it is checking if it is midnight "now". To be sure of not missing it the code needs to run continuously, which seems a bit inefficient to me. Although once every 30 seconds would still hit the window. More efficient and more elegant to check every once in a while whether midnight has gone past. A challenge might be the time format which depends on locale. (Americans like their AM / PM etc whereas we Euro weenies have our 24 hour format). So I'd store %date% as a string (e.g. set startdate=%date%) and just check every once in a while if it has changed yet (as it does at midnight every day). IF NOT "%startdate%"=="%date%" it's the next day.




Quote from: ghostdog74 on August 29, 2007, 12:44:05 AM
if that's the case, it would be simpler to just schedule a job at or after 12 midnight to query the existence of the file, instead of looping everytime.

Simpler and more logical.


Discussion

No Comment Found