WSH Command Line ArgumentsDriveSpace ExampleMuch like with Unix shell scripts, in Windows Script Host a script can use command line arguments. These can be useful when you wish to pass some information into a script. The drivespace.vbs example below is used at the command line by entering something like either of the following:
The first command uses cscript to run drivespace.vbs with c, p, and w as the
command line parameters (arguments). The program then reports on the amount
of free disk space for each of these three drives. The second command
uses wscript to run drivespace.vbs and has c, w, and h as the command line parameters.
WScript.Arguments is the collection containing the information
on the arguments. Like all collections it has a Count property
and an Item property, both of which are used above. Note how the
Count is checked to see if it is zero. If so, we display an error
message and quit the program. Item is an array of strings, and
Item(k) gives a string containing the value supplied by the user as argument k.
The FileSystemObject's GetDrive method is used to produce a drive object corresponding to a given drive letter. A user-written procedure is then used to calculate the amount of free space on this drive and append to the msg string a string containing this free space information. Note that this is a good example of how to create a Sub procedure with parameters. Parameters are passed by reference by default, so that any changes made in the procedure are sent back out. This FindSpace procedure also has two local variables,
space and units .
The code inside of this Sub procedure illustrates an if .. elseif .. else .. end if
construct. Also, the Round function is used to round a number to 2 decimal
places (where the 2 is specified in the second parameter). Recall, too, that
the & is the string concatenation operator and the underscore indicates
that a line of code is being continued.
FileReport ExampleThis example reports information on the file or files supplied as command line arguments (parameters). See the usage section in the comments as well as the example of use. Suppose we try the script out by using the following in a DOS box (and suppose that the two files actually do exist):
Then the output from the script looks something like the following.
If you ran the script using wscript instead of cscript then these results
would be shown in a graphical window instead of as text in the DOS box as shown here.
You might also run this script to look for an executable or dll file.
Here is an example that looks for one of each:
Next, let's look at the script itself. In many ways it is similar to the
previous script, but it uses some new features.
By now you can probably understand this script's operation on your own. But just to be sure, here is a quick summary: As in the previous example, this script generates an error message and quits if the user failed to supply at least one parameter. Throughout the script a message string is built up in the variable cMsg. It is echoed at the end of the script. The loop goes through all of the arguments to the script by looking at args.Item(k) as k runs from 0 to num - 1. In each case, args.Item(k) is the pathname for a file. For example, it might be the "c:\autoexec.bat" mentioned in the comments. The GetFile method returns a File object containing information on this particular file. The script then use the name, DateLastModified, and Type properties on this File object to look up the obvious pieces of information about this file. Other useful properties of this object include path (for the complete pathname for the file), DateCreated, DateLastAccessed, and Size (for the size of the file in bytes). At the end of this script, Nothing is assigned into the objects to be sure that they are destroyed. More InformationFor further information see the References section. Back to main Windows Script Host page |