Why wont this batch script work?
Asked by
XOIIO (
18360
)
January 3rd, 2011
I am making an alarm/timer script, and this line isn’t working.:
IF ’%TIME%’==‘0:18:05.20’ (GOTO yes) ELSE (GOTO LOOP)
It works when I put %DATE% in instead like this:
IF ’%DATE%’==‘03/01/2011’ (GOTO yes) ELSE (GOTO LOOP)
It quits really quickly with the first line, but i was able to see what is says, and it says
0:18:05.20 was unexpected at this time.
What’s going wrong?
Observing members:
0
Composing members:
0
6 Answers
It sounds as though you aren’t running the “command” shell in interactive mode (by running “command” from the Run… dialog) which will enable you to see (and copy/paste) these error messages and assist in debugging in other ways. It’s a lot easier for folks to answer questions of this type if error messages are included verbatim without paraphrasing.
I don’t know command.com scripting very well, so I have to guess… and my guess is that the %TIME% variable is picky about its format: try prepending a leading 0 like this:
‘00:18;05.20’ instead of ‘0:18:05.20’
HTH
That might help, but for some reason it shows something like this.
01:12:56.45’ ’==‘00:18:05.20’ was not expected at this time.
When I put it in like this
IF ‘TIME’==‘02:57:00.00’ (GOTO yes) ELSE (GOTO LOOP)
is stays open and echoes waiting part of the script but when the time changed it didn’t help.
I get the same error on both lines:
C:\USERS\MRITTY>IF ’%TIME%’==‘8:21:35’ (echo ‘Yes’) ELSE (echo ‘No’)
8:21:31.80’==‘8:21:35’ was unexpected at this time.
C:\USERS\MRITTY>IF ’%DATE%’==‘03/01/2011’ (echo ‘Yes’) ELSE (echo ‘No’)
01/03/2011’==‘03/01/2011’ was unexpected at this time.
Note that if I change the single quotes to double quotes, it all works fine:
C:\Users\mritty>IF ”%TIME%”==“8:21:35” (echo Yes) ELSE (echo No)
No
C:\Users\mritty>IF ”%DATE%”==“03/01/2011” (echo Yes) ELSE (echo No)
No
Someone on instructables my old home figured that out too, I’m not sure why, but it works.
Answer this question 
This question is in the General Section. Responses must be helpful and on-topic.