Using the WScript Object
Windows Script Host 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.3, that you may find useful in writing scripts.
Reference List 3.3 Properties and Methods of the WScript Object.
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
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'll discuss arguments in more detail in Chapter 10, "Deploying Scripts for Network Management."
Returns the full path and filename of the Windows Script Host program that is running your script (for example, "c:\Windows\System32\cscript.exe").
A Boolean value: True if the script is running in Interactive mode and False if in Batch mode. You may set this property using the //I or //B switch on the command line, or you may directly set the value in a script (for example, WScript.Interactive = False). In Batch mode, message and input boxes will not appear.
Returns the name of the script host program (for example, "Windows Script Host").
Returns the name of the directory containing the script host program (for example, "c:\Windows\System32").
Returns the full path and name of your script file (for example, "c:\test\myscript.vbs").
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'll discuss these in Chapter 4. Available with Cscript only.
Returns the version of Windows Script Host (for example, "Version 5.1").
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.)
Connects an existing object to the script using event handler functions whose names begin with the string prefix.
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 may 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 will be illustrated extensively in Chapters 7 and 8.
Terminates the script. If a numeric value is specified, it is returned as the process's exit codethis can be useful when running scripts from batch files.
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 is the Arguments property.
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, and 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, you can write the script to get the filenames from its command line, so you can simply type something like this:
C:\> cscript myscript.vbs some.doc another.doc
Usually, each programming language has its own way of providing command-line arguments to a program, but in the Windows Script Host 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'll 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 the command line
C:\> cscript myscript.vbs some.doc another.doc
the WScript.Arguments collection will have two items: some.doc and another.doc. In VBScript, the for each statement will let your script process them in turn.
If you don't specify any command-line arguments, though, this script will do 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'll find to be very handy.
When a script uses command-line arguments to specify what files (or users, computers, or whatever) to work with, it should at the very least explain how to use the script if no arguments are specified:if WScript.arguments.length = 0 WScript.echo "This script processes the named files" WScript.echo "by doing etc etc etc to them". WScript.echo "Usage: myscript file [file ...]" WScript.quit end if for each filename in WScript.arguments 'process filename ... next
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 that the subroutine can be called with the default file or with specified files. In VBScript, it would look like this:
if WScript.arguments.length = 0 process "default.file" else for each filename in WScript.arguments process filename next end if sub process (filename) ' statements to process filename ... end sub
In Chapter 10 I'll show how to use more powerful types of command-line processing.