How can I pass and access command line arguments in VBscript?
3 Answers
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 Comment
Alexander Bird
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".If you need direct access:
WScript.Arguments.Item(0)
WScript.Arguments.Item(1)
...
2 Comments
Simon Sellick
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
You might want to use
WScript.Arguments.Count with this.@Jerther is almost correct. Direct access is done like so:
WScript.Arguments(0)
WScript.Arguments(1)
(I would have just commented on their answer, but am not deemed reputable enough.) Anyway, this is how I had to write it to get it to work.
2 Comments
Stephen Quan
Actually, both you and Jerther are correct. There IS an Item property on the WScript.Argument interface and it is also a default property. It is your choice whether to give it explicitly as per Jerther's answer or to omit it and it will be used implicitly, as in your case. If you're interested see DISPID_VALUE in learn.microsoft.com/en-us/previous-versions/windows/desktop/…
For Serious
All I know is the script does not work with WScript.Arguments.Item(0) in it, and does without it.