Visual Basic – Calling a Batch File Through VB Script (VBS)

--> (Word) --> (PDF) --> (Epub)
This article has been published [fromdate]

There would be some situations where we need to call batch file from [gs script]s. Here the script to call: INSTALL.bat;

  1. Copy the below code to Notepad (or equivalent text editor, like Notepad++ or other).

    Function Execute()
    
    dim shell
    set shell=createobject("wscript.shell")
    shell.run "INSTALL.bat"
    set shell=nothing
    End Function
  2. Save the file as .VBS;

A small Addition

For the Run method:

Shell.Run "INSTALL.bat"

It's good to trap any potential errors into a variable and to tell the method not to execute the next line until the current process is complete.

errTrap = Shell.Run("INSTALL.BAT", 1, True)

This will return any error codes into errTrap. From there you can log the error or act on it accordingly in the [gs script].

  • "True" tells wscript/cscript to "Wait until this command is complete before running the next line." The "1" is just the window style. Not that important.

Whenever you are returning values you have to put the arguments into parenthesis.

An addition Script

Following is the template that not only return an [gs exit code] but also writes to the Event Log.

'-------------------------------------------------- 

'GET CURRENT FOLDER PATH
sLogHeader = "Install batch file"
path = FileSystemObject.GetParentFolderName(WScript.ScriptFullName)
If Right(path, 1) <> "\" Then path = path & "\"
End If
errTrap = shell.Run(path, 1 ,True)
If (errTrap = 0) Or (errTrap = 3010) Then
'WRITE EXIT CODE [0-success/3010-success&requires reboot] TO EVENTLOG
shell.LogEvent vbLogSuccess, sLogHeader & "Installation completed successfully." & VbCrLf & "Exit code: " & errTrap
WScript.Quit (errTrap)
Else
'WRITE EXIT CODE TO EVENTLOG
shell.LogEvent vbLogError, sLogHeader & "Installation failed." & VbCrLf & "Exit code: " & errTrap WScript.Quit (errTrap)
End If
Set FileSystemObject = Nothing
Set Shell = Nothing
'--------------------------------------------------

SOURCE

LINK (Symantec.com)

LANGUAGE
ENGLISH

1 thought on “Visual Basic – Calling a Batch File Through VB Script (VBS)”

Comments are closed.