VBScript command line arguments

Positional arguments

Calling a script with unnamed arguments is a simple method of passing values into a VBScript, those values can then be picked up within the script by reading the properties of WScript.Arguments

cscript.exe demo.vbs December 500

[demo.vbs]
strMonth = WScript.Arguments.Item(0)
strPrice = WScript.Arguments.Item(1)
wscript.echo strMonth & " and " & strPrice

This can be extended to cope with a large number of arguments .Item(n) but every time the script is called all of the arguments must be present and passed in the same order. If any are missed out then the numbers will change .Item(4) becomes .Item(3)etc.

Named arguments

A named argument begins with a slash (/), and the name and the value are separated by a colon (:) those values can then be picked up within the script by reading the collection WScript.Arguments.Named

cscript.exe demo2.vbs /month:April /price:500

[demo2.vbs]
Set colArgs = WScript.Arguments.Named
strMonth = colArgs.Item("month")
strPrice = colArgs.Item("price")
wscript.echo strMonth & " and " & strPrice

Named arguments can be given in any order

cscript.exe demo2.vbs /price:500 /month:April

Named arguments are optional, so you can include a default:

If colArgs.Exists("month") Then  
   strMonth = colArgs.Item("month") 
Else  
   strMonth = "January" 
End If

To count the number of arguments:

intCount = WScript.Arguments.Count
wscript.echo intCount

“Art, in itself, is an attempt to bring order out of chaos” ~ Stephen Sondheim

Related

Arguments - Command line arguments
Variables - Define VBScript constants and variables
Windows 2000 Scripting Guide - Technet


© Copyright SS64.com 1999-2014
Some rights reserved