Reputation: 1352
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?
Upvotes: 0
Views: 141
Reputation:
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
Upvotes: 0
Reputation: 30113
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
WMIC.exe
: Windows Management Instrumentation Commandlocaldatetime
: Win32_OperatingSystem class property in CIM_DATETIME formatset "myTime=%myTime:~8,6%"
: Extract part of a variable (substring)Upvotes: 1