Program Flow

WSH supports two kinds of script files: simple script files, which were supported by WSH 1.0 and are suitable for simple scripting applications; and script files with XML, which are more structured, far more powerful, and have a number of features of interest to more advanced programmers. In this section, we'll examine how both types of script files can be used.

7.3.1 Simple Script Files

Simple script files written in VBScript usually have a .vbs extension and contain only VBScript language elements, along with references to the properties, methods, and events belonging to objects instantiated by the script. XML tags are not permitted within simple script files.

The program entry point of a simple script is the global area at the top of the file, and program execution terminates after the last line of code that is not contained within a function or a procedure has executed. This is illustrated by the simple script in Example 7-1. Program flow begins with the Dim statement on the first line and ends with the MsgBox function call on the fourth line. The fourth line also causes the AddTwo user-defined function to be executed before the MsgBox function. The MultTwo function is never executed, since it is not explicitly called by the first four lines of code.

Example 7-1. Program flow in a simple WSH script

Dim iVar1, iVar2

iVar1 = 1
iVar2 = 2
MsgBox AddTwo(iVar1, iVar2)

' Multiplies two numbers
Function MultTwo(var1, var2)
 MultTwo = var1 * var2
End Function

' Adds two numbers
Function AddTwo(var1, var2)
 AddTwo = var1 + var2
End Function

This top-down flow of control in WSH scripts has a very important implication: all variables defined outside of subroutines in a script file are global variables; that is, they are globally available to all of the routines stored in the file. To see what this means, let's take a look at the code in Example 7-2. The variable fs, which represents a FileSystemObject object, is automatically visible to the ShowStorage routine; it does not have to be passed as a parameter in order to be visible to the routine.

Example 7-2. WSH global variables

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")


Public Sub ShowStorage( )
 Dim strMsg, dr

 For Each dr In fs.Drives
 strMsg = strMsg & dr.DriveLetter & ": " & _
 dr.FreeSpace & vbcrlf

 MsgBox strMsg
End Sub

This means, of course, that any modifications to the variable that the routine makes, whether they are deliberate or inadvertent, are reflected in the variable's value once control returns to the main code block.

To prevent this, parameter lists can be used to make sure that arguments are explicitly passed to called routines. If those arguments are passed by value, using the ByVal keyword, their value will remain unchanged when control returns to the calling routine, even if their value has been changed in a subordinate routine. This is illustrated by the script in Example 7-3, which assigns the values 5 and 10 to two variablesintX and intY, respectivelybefore calling AddTwo with these two variables as arguments. AddTwo contains a common assignment error: rather than assigning the sum of the arguments to a new variable, the sum is assigned to the intX parameter, effectively overwriting its value. However, when control returns to the calling routine and the sum of intX and intY is displayed, it remains as it was before the call to the AddTwo subroutine, since the changes made to the value of intX in AddTwo does not affect its value once the routine ends.

Example 7-3. Passing arguments by value

Dim intX, intY

intX = 5
intY = 10

AddTwo intX, intY

MsgBox "intX + intY = " & intX + intY

Sub AddTwo(ByVal intX, ByVal intY)
 intX = intX + intY
 MsgBox intX
End Sub

7.3.2 Script Files with XML Code

As of Version 2.0, WSH allows you to create .wsf files, which must contain one or more jobs that are designated by the XML <job>... tag. In addition, you can include files containing script by using the tag and including its src attribute. (See Section 7.5 later in this chapter for details on XML tags.)

Any script file with a .wsf extension must contain the XML tags needed for the script to run. At a minimum, these are the ... and, if script is present, the tags.

In a .wsf file that contains multiple jobs, each job is independent of others. In other words, the public variables and the public subroutines and functions of one job are not available to other jobs in the same .wsf file. Program flow begins at the beginning of the global script in the job designated by the //Job: switch when the script was launched, and continues until the tag is encountered. If the //Job: switch was not used, program flow begins with the global script belonging to the first job in the file.

If a script file is included using the src attribute of the

Vbscript in a Nutshell
VBScript in a Nutshell, 2nd Edition
ISBN: 0596004885
EAN: 2147483647
Year: 2003
Pages: 335
Simiral book on Amazon © 2008-2017.
If you may any questions please contact us: