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 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, param1 and param2, are both passed to the script as command-line parameters when it is run.

The problem with providing command-line arguments to your script is that Windows considers scripts to be documents instead of executables. This means that you can’t run a script with a command-line parameter without typing; you can’t drag a file onto a script icon or put a script directly into your Send To menu. ...

Get Windows Me Annoyances now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.