VBScript - Using command line parameters

By Bryan Muehlberger, ITworld |  How-to

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]
[server3] ..."
WScript.Quit
End If

'Print Each Command Line Argument Separately
For i = 0 to (WScript.Arguments.Count - 1)
Wscript.Echo "Argument number " & i & " is: " & WScript.Arguments(i)
Next
-------------------- 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
following:

cscript c:\scripts\CommandLine.vbs

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.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question
randomness