0

Basically what I'm trying to create is a script that only will run if the time is LEQ than 19:00 (7 PM), what I did so far is:

set myTime=%time%
set myFlag=false
if %myTime% LEQ 19:00 set myFlag=true
if myFlag=true (
*my script*
)

It returns this error message: "86 was not expected at this moment", (86 being the last numbers of the variable 'myTime' [14:36:11,86]. It just won't work.

I've also tried:

set myTime=%time%
set myFlag=false
if %myTime% LEQ 19:00:00,00 set myFlag=true
if myFlag=true (
*my script*
)

Both ways i get the same error message double-digit number not expected. Any thoughts on how to solve this? It's even possible do a time comparision on a windows batch file?

Mitrek
  • 902
  • 6
  • 9
  • There is no (native) way in batch programming to do real timestamp comparisons. You can compare only strings and numbers; something like `19:00` or `19:00:00,00` is treated as string. However, if your time format leads to values with fixed length, you can compare them like strings to achieve the expected result. To avoid trouble with the `,` character, enclose the comparison expressions in quotation marks, like: `if "%myTime%" LEQ "19:00:00,00" ` – aschipfl May 29 '16 at 19:37

2 Answers2

1

Syntax: Escape Characters, Delimiters and Quotes

Delimiters

Delimiters separate one parameter from the next - they split the command line up into words.

Parameters are most often separated by spaces, but any of the following are also valid delimiters:

  • Comma (,)
  • Semicolon (;)
  • Equals (=)
  • Space ( )
  • Tab ( )

Notice that although / and - are commonly used to separate command options, they are absent from the list above. This is because batch file parameters are passed to CMD.exe which can accept it's own parameters (which are invoked using / and - )

Next code snippet should work (see set /? and if /?):

set "myTime=%time%"
set "myFlag=false"
if "%myTime%" LEQ "19:00:00,00" set "myFlag=true"
if "%myFlag%"=="true" (
   echo *my script*
)

Note that above code snippet is locale dependent. You can try next locale independent solution similar to this answer to another question:

SETLOCAL EnableExtensions
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "myTime=%%a"
set "myTime=%myTime:~8,6%"
set "myFlag=false"
if "%myTime%" LEQ "190000" set "myFlag=true"
if "%myFlag%"=="true" (
   echo *my script*
)

Read

Community
  • 1
  • 1
JosefZ
  • 22,747
  • 4
  • 38
  • 62
0
Set Ag=WScript.Arguments

If CDate(Ag(0)) > CDate(Ag(1)) then 
    Wscript.echo "Param 1 greater than Param 2"
    wscript.quit 0
else
    Wscript.echo "Param 1 NOT greater than Param 2"
    wscript.quit 1
End If

To use in batch (use any valid time/date string)

scriptname.vbs 7pm 8pm
If errorlevel 0 if not errorlevel 1 echo Param 1 greater than param 2
If errorlevel 1 if not errorlevel 2 echo Param 2 greater than param 1

Or

scriptname.vbs 19:00:00 20:00:00
If errorlevel 0 if not errorlevel 1 echo Param 1 greater than param 2
If errorlevel 1 if not errorlevel 2 echo Param 2 greater than param 1