Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: Unknown Number of Parameters - uses the shift command[Next]: WMIC.exe - WMI From Windows Command Line (batch files)
\->Batch Files->WaitForTime() - delay until a chosen time

WaitForTime() [delay until a chosen time].cmd.vbs.txt

The batch file sample below contains VBSCRIPT (is created at run time) imbedded into it.

The "VBS4CMD.VBS" script was used to turn the tested VBSCRIPT into the batch file code to create it. That code was further tweeked.

If you want to change the code its probably easiest to let the batch file create the code and comment out the deletion. You will then be able to test and improve the code before running it through "VBS4CMD.VBS" again.

Depending on your code you may want to create the VBS once and use many times or create each time.

[anchor]

The Code for: "WaitForTime() [delay until a chosen time].cmd.vbs.txt"

This is the example, a shortcut was installed to this code :

@echo off
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
:: $Header:   C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[cmd].WaitForTime() [delay until a chosen time].cmd.vbs.txt.pvcs   1.0   11 Jul 2014 19:31:04   USER "Dennis"  $
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
setlocal


call :WaitForTime "2:15pm"
goto :EOF



@REM **************[v09.134]******************
:WaitForTime
@REM *****************************************
    set  WaitRcSeconds=%~2
    if   "%WaitRcSeconds%" == "" set WaitRcSeconds=60
    set WaitFlag=%BatchFileDir%\WaitForTime.FLG.TXT
    set  VbsFile=%TEMP%\WaitForTime%WaitRcSeconds%-%random%.vbs
    echo if Wscript.Arguments.Count ^<^> 1 then                                                                      > "%VbsFile%"
    echo    wscript.echo "Invalid parameters, expected 1 (the time to wait until), got " ^& Wscript.Arguments.Count >> "%VbsFile%"
    echo    wscript.quit 777                                                                                        >> "%VbsFile%"
    echo end if                                                                                                     >> "%VbsFile%"
    echo dim NowTime        : NowTime       = now()                                                                 >> "%VbsFile%"
    echo dim TimeArg        : TimeArg       = trim(Wscript.Arguments(0))                                            >> "%VbsFile%"
    echo.                                                                                                           >> "%VbsFile%"
    echo if left(TimeArg, 1) = "+" then                                                                             >> "%VbsFile%"
    echo    dim Seconds2Delay : Seconds2Delay = trim(mid(TimeArg, 2))                                               >> "%VbsFile%"
    echo    wscript.echo "We have been told to wait for """ ^& Seconds2Delay ^& """ seconds"                        >> "%VbsFile%"
    echo    wscript.sleep cint(Seconds2Delay) * 1000                                                                >> "%VbsFile%"
    echo    wscript.echo "   * Delay completed..."                                                                  >> "%VbsFile%"
    echo    wscript.quit 0                                                                                          >> "%VbsFile%"
    echo end if                                                                                                     >> "%VbsFile%"
    echo.                                                                                                           >> "%VbsFile%"
    echo wscript.echo "We have been told to wait until """ ^& TimeArg ^& """"                                       >> "%VbsFile%"
    echo on error resume next                                                                                       >> "%VbsFile%"
    echo dim WaitUntilTime : WaitUntilTime = cdate(TimeArg)                                                         >> "%VbsFile%"
    echo if err.number ^<^> 0 then                                                                                  >> "%VbsFile%"
    echo    wscript.echo "   * ERROR: The passed time is not in a valid format...  Wait aborted!"                   >> "%VbsFile%"
    echo    wscript.quit 666                                                                                        >> "%VbsFile%"
    echo end if                                                                                                     >> "%VbsFile%"
    echo on error goto 0                                                                                            >> "%VbsFile%"
    echo if datepart("yyyy", WaitUntilTime) ^< 2009 then                                                            >> "%VbsFile%"
    echo    dim TodayDate : TodayDate = datepart("yyyy", NowTime) ^& "/" ^&  LPad0(datepart("m", NowTime), 2) ^& "/" ^&  LPad0(datepart("d", NowTime), 2)  >> "%VbsFile%"
    echo    TimeArg = TodayDate ^& " " ^& TimeArg                                                                   >> "%VbsFile%"
    echo    wscript.echo "Until time didn't include date, have adjusted this to """ ^& TimeArg ^& """"              >> "%VbsFile%"
    echo end if                                                                                                     >> "%VbsFile%"
    echo WaitUntilTime = cdate(TimeArg)                                                                             >> "%VbsFile%"
    echo.                                                                                                           >> "%VbsFile%"
    echo wscript.echo ""                                                                                            >> "%VbsFile%"
    echo wscript.echo "It is now " ^& NowTime ^& ", we will wait until " ^& WaitUntilTime                           >> "%VbsFile%"
    echo dim DiffInSeconds : DiffInSeconds = datediff("s", NowTime, WaitUntilTime)                                  >> "%VbsFile%"
    echo if DiffInSeconds ^< 0 then                                                                                 >> "%VbsFile%"
    echo    wscript.echo "    * We have already passed that time!"                                                  >> "%VbsFile%"
    echo else                                                                                                       >> "%VbsFile%"
    echo    do while DiffInSeconds ^> 0                                                                             >> "%VbsFile%"
    echo        wscript.sleep %WaitRcSeconds%000                                                                    >> "%VbsFile%"
    echo        NowTime       = now()                                                                               >> "%VbsFile%"
    echo        DiffInSeconds = datediff("s", NowTime, WaitUntilTime)                                               >> "%VbsFile%"
    echo    loop                                                                                                    >> "%VbsFile%"
    echo end if                                                                                                     >> "%VbsFile%"
    echo wscript.echo "Finished wait at " ^& NowTime                                                                >> "%VbsFile%"
    echo.                                                                                                           >> "%VbsFile%"
    echo '============================================================================                              >> "%VbsFile%"
    echo function LPad0(Str, PadLen)                                                                                >> "%VbsFile%"
    echo '============================================================================                              >> "%VbsFile%"
    echo     if  len(Str) ^>= PadLen then                                                                           >> "%VbsFile%"
    echo         LPad0 = Str                                                                                        >> "%VbsFile%"
    echo    else                                                                                                    >> "%VbsFile%"
    echo        LPad0 = right(string(PadLen, "0") ^& Str, PadLen)                                                   >> "%VbsFile%"
    echo    end if                                                                                                  >> "%VbsFile%"
    echo    end function                                                                                            >> "%VbsFile%"
    call :SaySection Waiting until "%~1"
    echo Waiting until "%~1" > "%WaitFlag%"
    cscript.exe //NoLogo "%VbsFile%" "%~1"
    del "%VbsFile%"  >nul 2>&1
    del "%WaitFlag%" >nul 2>&1
    set  WaitRcSeconds=
    set       WaitFlag=
    set        VbsFile=
    call :SAY Waiting completed
    goto :EOF

Please note that that I am not trying to show how great I am by producing batch files 9,000 characters long on one line that no one will understand or be able to debug when they go wrong. I am going out of my way to comment the code and make it verbose so beginners and advanced users will both benefit. I don't claim to be an expert that knows everything, if I'm wrong or make a mistake then please contact me and let me know :-)


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: Unknown Number of Parameters - uses the shift command[Next]: WMIC.exe - WMI From Windows Command Line (batch files)


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.