August 11, 2003, 12:00 AM — This week we review how to use command line parameters within VBScript.
While writing scripts you may sometimes want to pass options or
information from the command line directly to your program. This can be
easily accomplished using the 'Wscript.Arguments' property within
Windows Scripting Host.
The 'Wscript.Arguments' property allows you to obtain information passed
to you through the command line. You can obtain the number of items on
the command line, in addition to the individual items themselves.
Let's see how you can do this. Let's assume that your script requires
two command line parameters. The first is the name of a file that you
want to read from, and the other is the name of a file to write your
output data to.
Here is the code for the example:
------------------- Begin Code Snippet -------------------
If WScript.Arguments.Count <> 2 Then
Wscript.Echo "Usage: CScript.exe commandLine.vbs
'Print Each Command Line Argument Separately
For i = 0 to (WScript.Arguments.Count - 1) 'Beginning of my For