83

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

2 Answers 2

91
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
1
  • 21
    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". Commented Aug 6, 2013 at 19:24
62

If you need direct access:

WScript.Arguments.Item(0)
WScript.Arguments.Item(1)
...
2
  • 4
    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. Commented Apr 16, 2016 at 13:07
  • 7
    You might want to use WScript.Arguments.Count with this.
    – BuvinJ
    Commented Dec 9, 2016 at 16:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.