Search notes:

VBScript: arguments

Iterate over arguments

The following simple script iterates over each argument given to a (console) VBScript script:
option explicit

wscript.echo "You gave " & wscript.arguments.count & " arguments."

dim argNo

for argNo = 1 to wScript.arguments.count
    wscript.echo "Argument " & argNo & " is: " & wScript.arguments(argNo-1)
next
Github repository about-VBScript, path: /language/arguments/iterate.vbs
It might be invoked such as
c:\> cscript iterate.vbs hello world etc.
Note, if a named argument is used (/val:42), this counts as one argument.

Checking for named arguments

The following script checks which of the named arguments foo, bar and baz is given to a script.
Named arguments have the form /argName:value.
In order to explicitly give no value, /argName: might be used.
option explicit

dim namedArgs
set namedArgs = wscript.arguments.named


dim paramName
for each paramName in array("foo", "bar", "baz")

  '
  ' Check if paramName was specified when script
  ' was invoked:
  '

    if namedArgs.exists(paramName) then
       wscript.echo "Value for " & paramName & " = " & namedArgs(paramName)
    else
       wscript.echo "No value given for " & paramName
    end if

next
Github repository about-VBScript, path: /language/arguments/named.vbs

Optional arguments

In order for /opt arguments to be truly optional, the other (mandatory) arguments must be accessed via arguments.unnamed(…).
option explicit

wscript.echo "1st non-optional paramter: " & wscript.arguments.unnamed(0)
wscript.echo "2nd non-optional paramter: " & wscript.arguments.unnamed(1)

if wscript.arguments.named.exists("opt") then
   wscript.echo "opt was chosen"
end if
Github repository about-VBScript, path: /language/arguments/optional-arguments.vbs

See also

VBScript

Index