A Windows Script Host (WSH) script can use command line arguments. These can be useful when you wish to pass some information into a script. The test.vbs example below is used at the command line by entering something like either of the following:
cscript test.vbs a b c
wscript test.vbs a b c
There are very few differences between cscript and wscript. Cscript is a console-based application and runs inside an MS-DOS box. The first command uses cscript to run test.vbs with a, b, and c as the command line parameters (arguments). The second command uses wscript to run test.vbs and has a, b, c as the command line parameters.
To get the command line parameters inside the VBS file please use:
Code: Select all
set args = WScript.Arguments
num = args.Count
if num = 0 then
WScript.Echo "No command line parameters!"
for k = 0 to num - 1
WScript.Arguments is the collection containing the information on the arguments. Like all collections it has a Count property and an Item property, both of which are used above. Note how the Count is checked to see if it is zero. If so, we display an error message and quit the program. Item is an array of strings, and Item(k) gives a string containing the value supplied by the user as argument k.