Guy's Scripting Ezine 130 - Using VBScript Exit Codes
While these scripts have an important purpose, namely to master exit
codes, they are also an ideal vehicle for those with little experience, who wish to learn VBScript by experimenting with an easy-to-understand example. Follow my lead and go from zero to hero in 10 minutes.
Exit codes have been my Achilles heel. Naturally I have been aware of their existence, but I have just not had a killer reason to include this property
in my VBScripts. Consequently, when Keith Hubbard wrote in asking for an exit code example, my script cupboard was bare.
What made my day was when Keith Hubbard wrote again to report that he had solved his own
problem, moreover, he kindly enclosed a completed example illustrating exit code.
The whole point of trapping the exit code (.status =1) is so that your script will wait for one event to complete before continuing with the next
section of code. Not only is this good programming practice, but also it solves the problem of dependency.
One other technique to look out for, is the loop that keeps testing:
'Has the program exited yet?'. As may already know, I preach building scripts gradually, thus this script just focuses on one task, waiting for an event to occur. In the production script
instead of a WScript.echo message, we would write code to delivering the main payload of our project. Keith also points out that this technique of waiting for the exit code is efficient
because it saves CPU cycles and memory.
You need a machine with the Calc (Calculator) program installed. Alternatively, edit "Calc" to "Notepad", or any executable that you can launch via the Run dialog box.
Instructions to test the exit code .Status
Save the following script with a .vbs extension, for example exit.vbs
Navigate to the folder where you saved the .vbs file, double click the file.
Note a good VBScript editor will help you identify VBScript methods and properties, for example .status, .exit and .quit. Get your
free trial of OnScript
' Exit.vbs ' VBScript to demonstrate exit code. ' Authors Keith Hubbard and Guy Thomas ' Version 1.2 - November 2006 Option Explicit Dim objShell, objExec Set objShell =
CreateObject("WScript.Shell") Set objExec = objShell.Exec("Calc") Do While objExec.Status = 0 WScript.Sleep 100 Loop WScript.Echo "Calc Closed Status = " & objExec.Status
WScript.Quit
Learning Points
Note 1:
After the header, the script creates a Shell object called objShell. From this master object we create objExec which actually launches the program.
Note 2: Pay close attention
to the .Exec method as it underpins this script. There a number of ways to launch a program, but the .Exec method brings the .Status method into play.
Note 3: objExec.Status obtains the value for the exit code. A value of zero means still open, hence the script loops until the value changes from zero to one.
Note 4: This script waits or sleeps for 100 milliseconds. In example 2 I chose a value of 500 meaning half a second. Feel free to experiment with WScript.Sleep 100.
Note 5:
For this test script, the code lacks a realistic payload. WScript.echo does not perform any useful work, however, we will improve on this limitation in Example 2 below.
Note 6: Thanks to Tom
Riedy, Version 1.2 now has Option Explicit added. This exposed a DIM error which we have now corrected.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
It is
my belief that if you find two ways of doing the same thing - not difficult with VBScript - then you get what I call binocular vision and thus achieve better understanding of VBScript's methods and
syntax.
What this script does is introduce variables to control the applications. Moreover there is a minor payload in that when you close the first program (Calc), the second program (Notepad)
launches. However, Notepad always waits for as long as it takes Calc to close, before executing.
Instructions
Save the following script with a .vbs extension, for example exit2.vbs
Navigate to the folder where you saved the .vbs file, double click the file.
Note a good VBScript editor will help you with the line numbers. Get your
free trial of OnScript
' Exit2.vbs ' VBScript to demonstrate waiting with exit code. ' Authors Keith Hubbard and Guy Thomas ' Version 2.2 - November 2006 Option Explicit Dim objShell, objExec, strPrelim, strMainProg strPrelim =
"Calc" strMainProg ="Notepad" Set objShell = CreateObject("WScript.Shell") Set objExec = objShell.Exec(strPrelim) Do While objExec.Status = 0 WScript.Sleep 500 Loop WScript.Echo
strPrelim & " just closed. Click to launch "_ & strMainProg Set objExec = objShell.Exec(strMainProg) WScript.Quit
Learning Points
Note 1: My biggest worry is
that the underscore (_) will not work properly when you copy my example. What the (_) does is to allow one instruction to word-wrap over two lines. Any problem with line 13, then substitute this:
WScript.Echo strPrelim & " just closed. Click to launch " & strMainProg
Note 2: Observe how the script introduces two variables strPrelim and strMainProg
Note 3: Study this sequence, a) strPrelim (Calc) executes, b) then the Do While loop checks the status. c) Only when the status changes from zero to one does the payload strMainProg execute.
Note 4: Tom Riedy prompted me to add Option Explicit. As a result the above version number is 2.2, moreover the variables objShell, objExec, are now declared correctly, and I have removed objCalc
as it was not doing anything useful.
Objects have properties. The .Status property can have a value of zero when a program is running, but when it closes the value changes
to one. A neat script can trap the value of the exit code and then take appropriate action. In practical terms mastering the exit codes means your scripts can wait for one action to complete before the next
program launches. This logical, measured code makes for an orderly, well behaved script - just what we want.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.