9.8 Using Command-Line Parameters in Scripts

A command-line parameter is a bit of text specified after the filename of a script when it is executed from a command prompt (see the following examples). The function used to convert a single command-line parameter into a variable is the following:

Function CommandLine(Number)   Set Arguments = WScript.Arguments   If Number <= Arguments.Count Then      CommandLine = Arguments(Number - 1)   Else     CommandLine = ""   End If End Function

For example, to display the second command-line parameter passed to a script, issue the following statement:

MsgBox CommandLine(2)

Although the command line may seem to be an antiquated concept, it's still very much a part of Windows. When you double-click on a .vbs file, for example, Windows actually executes the following command:

wscript.exe filename.vbs

where filename.vbs (the file that was double-clicked) is the command-line parameter for wscript.exe, telling it which script to run. Scripts also accept command-line parameters, which is accomplished like this:

wscript.exe filename.vbs param1 param2

The two additional parameters,[2] param1 and param2, are both passed to the script as command-line parameters, and can be retrieved during run-time by referencing CommandLine(1) and CommandLine(2), respectively.

[2] You can have as many or as few parameters as you like.

One of the most common uses of command-line parameters in scripts is to accept filenames, and there are two circumstances when this is most useful:

  • Drag one or more items onto the script file icon. Note that this didn't work in earlier versions of Windows, as scripts were considered to be documents instead of programs.

  • Place the script in your Send To folder. Then, right-click one or more items in Explorer, select Send To, and then select the name of the script. You can also place the a shortcut to the script in your Send To folder, which eliminates the .vbs filename extension that would otherwise appear in the Send To menu.

In either case, the script is executed, and the names of the input file(s) are accessible as command-line parameters, one for each filename. The following example script displays the names of all the files and folders drag-dropped on the script icon:

Report = "" Set Arguments = WScript.Arguments For i = 1 to Arguments.Count   Report = Report + Arguments(i - 1) + vbCrLf Next Msgbox Report

The script starts off by clearing the Report variable, and then borrows some code from the CommandLine function listed earlier[3] to initialize the Arguments object and determine the number of dropped files. Next, a For...Next structure is used to run through the arguments, adding each one to the Report variable, followed by a linefeed (using vbCrLf, a handy built-in constant containing carriage-return and linefeed characters). Note that the Arguments array is zero-based (the first item is Arguments(0), the second is Arguments(1), and so-on), so we need to include the (i - 1) part to compensate. Lastly, a Msgbox command is used to display the list of dropped files.

[3] It's actually possible to use the CommandLine function here instead, but doing so would make the script more cumbersome. And exactly who are you going to impress with a cumbersome script?



Windows XP Annoyances
Fixing Windows XP Annoyances
ISBN: 0596100531
EAN: 2147483647
Year: 2005
Pages: 78
Authors: David A. Karp

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net