Visual Basic - Calling a Batch File Through VB Script (VBS) –> (Word) Visual Basic - Calling a Batch File Through VB Script (VBS) –> (PDF) Visual Basic - Calling a Batch File Through VB Script (VBS) –> (Epub)
This article has been published 2 years, 214 days ago

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
Visual Basic - Calling a Batch File Through VB Script (VBS) Visual Basic - Calling a Batch File Through VB Script (VBS)
Facebook Twitter Email Plusone Linkedin Stumbleupon Delicious Digg

  One Response to “Visual Basic – Calling a Batch File Through VB Script (VBS)”

  1. Google Chrome 28.0.1500.95 Google Chrome 28.0.1500.95 Windows 7 Windows 7
    Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36

    Visual Basic – Calling a Batch File Through VB Script (VBS) http://heelpbook.altervista.org/2012/visual-basic-calling-a-batch-file-through-vb-script-vbs/ #howto #tutorial #heelpbook

Add Comment Register



   
© 2011 - 2013 HeelpBook - Today is: 28/8/2014 Last updated: November 30, 2011 at 14:38 pm Get helped -