Using command line arguments in VBscript

148,630

Solution 1

Set args = Wscript.Arguments

For Each arg In args
  Wscript.Echo arg
Next

From a command prompt, run the script like this:

CSCRIPT MyScript.vbs 1 2 A B "Arg with spaces"

Will give results like this:

1
2
A
B
Arg with spaces

Solution 2

If you need direct access:

WScript.Arguments.Item(0)
WScript.Arguments.Item(1)
...
Share:
148,630

Related videos on Youtube

Sunil
Author by

Sunil

Working on C++, C, SIP, VOIP, H323

Updated on April 15, 2020

Comments

  • Sunil
    Sunil about 4 years

    How can I pass and access command line arguments in VBscript?

  • Alexander Bird
    Alexander Bird almost 11 years
    You can access it directly with WScript.Arguments.Item(0). Item 0 is not the command's name (as it is in other languages); in Aphoria's example above it would be the string "1".
  • Simon Sellick
    Simon Sellick about 8 years
    You can also drag and drop a file onto a script in Explorer, which will run the script with the first argument set to the file path and name.
  • BuvinJ
    BuvinJ over 7 years
    You might want to use WScript.Arguments.Count with this.
  • Joseph
    Joseph almost 6 years
    How would I pass an array object e.g. like a json object using this same approach i.e with WScript.Arguments?