Run a VBScript then use the numeric result

Top  Previous  Next

 

This example uses the EXEC command to launch the Windows Scripting Host to run a VB Script file that returns a number between 1 and 7 indicating the current day of the week. The script logic then uses this value to calculate the date of next Wednesday. If the VB Script file does not exist this script attempts to create it.

 

:check_scripting_host

SET ScriptingHost_Path = "c:\windows\SysWOW64\cscript.exe"

IFFILE ScriptingHost_Path GOTO check_vbscript_file

SET ScriptingHost_Path = "c:\windows\System32\cscript.exe"

 

:check_vbscript_file

SET VBScript_Path = %installdir & "\GetDay.vbs"

IFFILE VBScript_Path GOTO begin

 

:make_vbscript

WRITEFILE VBScript_Path "If Wscript.Arguments.Count = 0 Then"

WRITEFILE VBScript_Path "  val = -1" /append

WRITEFILE VBScript_Path "Else" /append

WRITEFILE VBScript_Path "  If IsDate(Wscript.Arguments(0)) Then" /append

WRITEFILE VBScript_Path "    val = Weekday(Wscript.Arguments(0))" /append

WRITEFILE VBScript_Path "  Else" /append

WRITEFILE VBScript_Path "    val = -1" /append

WRITEFILE VBScript_Path "  End If" /append

WRITEFILE VBScript_Path "End If" /append

WRITEFILE VBScript_Path "Wscript.Quit(val)" /append

IFERROR= $ERROR_SUCCESS GOTO begin

CONSOLEMSG ">>>> Fatal Error: VB Script file does not exist and attempt to write it failed."

DISPLAY VBScript_Path

STOP

 

:begin

SET MyDate = %date ;; set the starting date

set VBScript_Path = '"' + VBScript_Path + '"' ;; add quotes so DOS isn't confused by spaces

EXEC ScriptingHost_Path /passargs VBScript_Path MyDate

SET DayOfWeek = %lasterror ;; get return value from VBScript, should be 1-7

IFNUM<= DayOfWeek 0 GOTO vbscript_error

IFNUM> DayOfWeek 7 GOTO vbscript_error

IFNUM>= DayOfWeek 4 GOTO today_is_wednesday_or_later

 

:before_wednesday

SETNUM offset = 4 - DayOfWeek

GOTO calc

 

:today_is_wednesday_or_later

SETNUM offset = 7 - DayOfWeek

SETNUM offset = offset + 4

GOTO calc

 

:calc

DATEADD MyDate offset ;; add offset days to get date of next Wednesday

SET Result = ">>>> Next Wednesday is " + MyDate

CONSOLEMSG Result

STOP

 

:vbscript_error

CONSOLEMSG ">>>> Fatal Error: VB Script did not return a value between 1 and 7."

STOP

 

 

This above sample expects to find a VB Script file named GetDay.vbs in the Robo-FTP installation folder. The sample will attempt to create the file if it does not already exist but that file creation will fail if the current Windows user lacks the required permissions. If you need to create the file manually it should contain the following VB Script commands:

 

If Wscript.Arguments.Count = 0 Then

   val = -1

Else

   If IsDate(Wscript.Arguments(0)) Then

        val = Weekday(Wscript.Arguments(0))

   Else

        val = -1

   End If

End If

Wscript.Quit(val)

 

 

Related Commands: EXEC, IFFILE, WRITEFILE, DATEADD

See also: List of Sample Script Files, Script File Wizard, Script Programming, Script Commands