Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing arguments to vbscript

Status
Not open for further replies.

RealGazza

Technical User
Jan 31, 2002
2
GB
Hi

I am writing a little script using vbscript to perform a number of actions on some files I have. Now, I want to be able to pass a few arguments to this script.

If I was using batch, I could do 'mybatch.bat Arg1 Arg2' and refer to these with %1 and %2 within the script.

Is this possible to do within vbscript, e.g.
cscript myvbs.vbs Arg1 arg2

If so, how would I refer to these from within the script?

Thanks for your help
GWG
 
A little more research and I found

WScript.Arguments

A collection of the arguments passed to the script.

Cheers
GWG
 
This is a bigger topic than you might think. You really need to consult the WSH documentation.

One link to the web version of this doc is:


This gives one way to retrieve one kind of command-line parameter. WSH supports both named and unnamed parameters:

/in:arg <-named
&quot;c:\my\fle.txt&quot; <- unnamed


There are even XML elements you can define in a WSH script for command-line arguments (parameters):


This even lets you build &quot;command line help&quot; (using /?) into your script without much coding.

The simplest example might be:
Code:
For i = 0 to WScript.Arguments.Count-1
    WScript.Echo WScript.Arguments.Unnamed(i)
next i
Where each &quot;i&quot; indexed item corresponds to your old-style %1, %2, etc. except that these are 0-based as shown above.

Note that this gives both the named & unnamed arguments. You get at them separately via the objects:

WScript.WshNamed
WScript.WshUnnamed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top