Using the WScript Object
WSH provides a built-in object named WScript for all scripts in all languages. We've used its Echo method in many of the examples in this book. WScript has several other methods and properties, as listed in Reference List 3.2, that you might find useful in writing scripts.
Reference List 3.2. Properties and Methods of the WScript Object
Properties
Arguments
Returns a collection of WshArguments objects, representing the strings on the command line used to start WScript or Cscript. For example, if a script is started with the command
WScript myscript.vbs aaa bbb
or
myscript aaa bbb
then WScript.arguments.item(0) would yield "aaa" and WScript.arguments.item(1) would yield "bbb".WScript.arguments.length gives the number of arguments.
I discuss arguments in more detail in the next section.
BuildVersion
Returns a number identifying the current version of Windows Script Host. This number might vary between versions of Windows and as WSH is updated through Windows Update. I have seen WSH on Windows 7 return 0 for this property. I would not trust it to be usable.
FullName
Returns the full path and filename of the WSH program that is running your script (for example, c:\Windows\System32\cscript.exe).
Interactive
A Boolean value: True if the script is running in Interactive mode and False if in Batch mode. You might set this property using the //I or //B switch on the command line, or you might directly set the value in a script (for example, WScript.Interactive = False). In Batch mode, message and input boxes do not appear.
Name
Returns the name of the script host program (for example, "Windows Script Host").
Path
Returns the name of the directory containing the script host program (for example, "c:\Windows\System32").
ScriptFullName
Returns the full path and name of your script file (for example, "c:\test\myscript.vbs").
ScriptName
Returns the name of your script file (for example, "myscript.vbs").
StdErr, StdIn, and StdOut
These are file streams that can be used to read from the standard input or write to the standard output and error files. I discuss these in Chapter 4, "File and Registry Access." These properties are available with cscript only, not wscript.
Version
Returns the version of WSH (for example, "Version 5.7").
Methods
CreateObject( progid [ , prefix ] )
Similar to the built-in CreateObject function. With a prefix argument, it creates connected objects that can communicate events to the script. (Events are beyond the scope of this book.)
ConnectObject object , prefix
Connects an existing object to the script using event handler functions whose names begin with the string prefix .
DisconnectObject object
Disconnects the script from an object's events.
Echo arg [ , arg ] ...
Displays any number of arguments of any type, formatted as strings and separated by spaces. Cscript writes them to the standard output, whereas WScript displays them in a pop-up message box.
GetObject(filename [, progid][, prefix])
Creates an object based on information stored in a file (for example, a document). If progid is not specified, it is determined from the file type. prefix might be specified to connect object events to the script.
GetObject can also obtain a reference to a preexisting object by specifying a special name called a moniker. This is illustrated extensively in Chapters 7 and 8.
Quit [ errorcode ]
Terminates the script. If a numeric value is specified, it is returned as the process's exit code—this can be useful when running scripts from batch files.
Sleep msec
Causes the script to pause for msec milliseconds. For example, WScript.sleep 1000 pauses for one second.
Of the properties and methods listed, the most useful are the Echo and Arguments properties. Let's see how you can use arguments to control what a script does when you run it.
Retrieving Command-Line Arguments
The use of command-line arguments is a common way of specifying information to a script at the moment it's run. The most common use for this is to write scripts that manipulate files, user accounts, or computers. The script can be written in a generic way, so that you can specify the particular files, people, or what-have-you at the time you run the script. For example, a script to process a file could be written like this:
filename = "specialdocument.doc"
'statements to operate on the file named filename
However, if you wanted to use this script to work with a different file, you'd have to edit the script. If you want a more general-purpose script, write the script to get the filenames from its command line, so you can simply type something like this:
C:\> myscript some.doc another.doc
Then, the script will operate on the files whose names you typed, rather than on a file whose name is built in to the script.
Usually, each programming language has its own way of providing command-line arguments to a program, but in the WSH environment, there is only one way they are obtained—through the WScript object's Arguments property.
The WScript.Arguments property returns a collection of objects, one for each item listed on the script's command line. You can write a script to use these arguments this way, more or less:
for each filename in WScript.arguments
' statements to operate on the file named filename
next
Of course, you have to use whatever method of manipulating objects and collections is appropriate to the script language you're using (this example is in VBScript). With script myscript.vbs, the command line
C:\> myscript some.doc another.doc
sets up the WScript.Arguments collection with two items: some.doc and another.doc. In VBScript, the for each statement lets your script process them in turn.
If you don't specify any command-line arguments, though, this script does nothing at all. It's best to have a script tell the user how to use it properly in this case. Here's a scheme for writing command-line scripts that you might find to be handy.
Alternatively, you might want your script to operate on a default file if no files are named on the command line. Such a script should use a subroutine to do the actual processing of the files, so the subroutine can be called with either the default file or with specified files. In VBScript, it looks like this:
if WScript.arguments.length = 0 then
' no arguments on command line -- process file "default.file"
process "default.file"
else
' process each of the files named on the command line
for each filename in WScript.arguments
process filename
next
end if
sub process (filename)
' statements to process filename
end sub
In Chapter 9, I show you how to use more powerful types of command-line processing.