84

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

Mike G
  • 4,142
  • 9
  • 43
  • 64
Sunil
  • 1,901
  • 3
  • 17
  • 24

2 Answers2

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
aphoria
  • 19,178
  • 7
  • 59
  • 70
  • 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". – Alexander Bird Aug 06 '13 at 19:24
58

If you need direct access:

WScript.Arguments.Item(0)
WScript.Arguments.Item(1)
...
Jerther
  • 5,240
  • 7
  • 35
  • 55