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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1741107428, '3.142.174.100', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/VBScript/language/arguments/index(112): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78