Last week, we finished our introduction to Looping using For...Next
statements. As promised, this week we examine how to retrieve
information from the command line.
For our example, let's assume you want to pass a list of server names
from the command line to your program so that you can perform some
actions to each of these servers. You can also do this by reading from a
file (which we'll discuss in a future article), but for now, assume that
you only have 5-10 server names that you want to pass to your program.
To do this, we must take advantage of the 'Wscript.Arguments' object.
This object gathers all of the additional information from the command
line and parses it out for your use. To see how this works let's write
some code that shows us how this is accomplished.
Here is the code for the above example:
------------------- Begin Code Snippet -------------------
If WScript.Arguments.Count = 0 Then
Wscript.Echo "Usage: CScript.exe CommandLine.vbs server1 [server2]
'Print Each Command Line Argument Separately
For i = 0 to (WScript.Arguments.Count - 1)
Wscript.Echo "Argument number " & i & " is: " & WScript.Arguments(i)
-------------------- End Code Snippet --------------------
Now this simple program will just display each of the command line
arguments that you passed to your program. However, it is easy to add to
this sample code and use each of the arguments within your code by using
the statement 'Wscript.Aruguments(i)' where 'i' is the number of the
argument that you want to reference.
If you copy the above code snippet into a text file, save it as
c:\scripts\CommandLine.vbs, open a command prompt and then type the
You will see the following output:
------------------- Begin Output -------------------
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Argument number 0 is: serverName1
Argument number 1 is: serverName2
Argument number 2 is: serverName3
--------------------- End Output -------------------
Let's step through the code to see what is happening. The first section
of code contained within the familiar If...Then...Else statement just
verifies that some arguments have been passed to the script. If no
arguments have been passed, then a message on how to use the script is
displayed. It is recommended that this be used in all programs that
expect some sort of argument to be given by the user. It makes it easier
for the user of the script to know what to do.
The next part of code uses the familiar For...Next loop (see last week's
article). Basically, we loop through all of the command line arguments
one by one, until we reach the last one. Two important things to note.
First, arguments are indexed starting at 0. However, the
'Wscript.Arguments.Count' statement returns the number of arguments (in
our example 3). In order to reference all of the arguments starting at
0, we must subtract 1 from our Count to get the last argument. In other
words, the first argument on the command line is referenced by
'WScript.Arguments(0)', the second one by 'WScript.Arguments(1)', and so
After we have displayed all of our arguments, we exit the program.
As mentioned above, instead of just displaying our arguments, we could
have easily referenced each of them and performed any number of actions
using the argument.
Using arguments passed via the command line is very common to scripts
used by system administrators, so having the knowledge on how to do this
is very useful.
Next week we will begin discuss commenting your code.