|
Free Open Book
Windows XP Annoyances |
9.8 Using Command-Line Parameters in ScriptsA 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.
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:
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.
|
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |