All pastes #2068718 Raw Edit

Something

public text v1 · immutable
#2068718 ·published 2011-05-25 00:57 UTC
rendered paste body
'
' VB Script Document: Wsh Arguments object (and related) sample
'
' Usage:
' wscript [scriptpath\]scriptname.vbs argument [ arguments] /namedargument[:namedargumentvalue] [/namedargumentn[:"named argument value"]]  
' or
' cscript [scriptpath\]scriptname.vbs argument [ arguments] /namedargument[:namedargumentvalue] [/namedargumentn[:"named argument value"]]
'
' Note: arguments must be separated by a blank (space character)
'       arguments containing a blank must be enclosed in quotes: "rabbit ears"
'       The quotes will be removed in the WshNamed (WshUnnamed as well) collection. For an argument to be in the WshNamed collection, it must have been used on the command line. If the argument has no value (such as a simple argument or an empty string), the Item property returns an empty string. Requesting a non-existent named argument from the Item property causes an error. To check if an argument exists, use the Exists method.
'
' e.g.
' wscript scriptname.vbs stringargument "stringargument with blanks" /stringarg:"stringarg with spaces"
'
option explicit

Dim objArgsNamed, objArgsUnnamed, objArgsInall, strResult, iii
strResult = ""

Set objArgsInall = WScript.Arguments
strResult = strResult & "There are " & objArgsInall.Count & " arguments in all:" & vbNewLine
' All arguments are referenced by their order on the command line
For iii = 0 to objArgsInall.Count - 1
  strResult = strResult & objArgsInall(iii) & " = " & Wscript.Arguments.Item(iii) & vbNewLine
Next
strResult = strResult & "-" & vbNewLine

Set objArgsUnnamed = WScript.Arguments.Unnamed
strResult = strResult & "There are " & objArgsUnnamed.Count & " unnamed arguments:" & vbNewLine
' Unnamed arguments are referenced by their order on the command line
For iii = 0 to objArgsUnnamed.Count - 1
  strResult = strResult & objArgsUnnamed(iii) & " = " & Wscript.Arguments.Unnamed.Item(iii) & vbNewLine
Next
strResult = strResult & "-" & vbNewLine

Set objArgsNamed = WScript.Arguments.Named
strResult = strResult & "There are " & objArgsNamed.Count & " named arguments:" & vbNewLine
' Named arguments cannot be referenced by their order on the command line
' Named arguments must be referenced by their names
'Expected names of named argumets:
Dim arrExpectArg(1)
arrExpectArg(0) = "stringarg"
arrExpectArg(1) = "otherArg"
For iii = 0 to UBound(arrExpectArg,1)
  If WScript.Arguments.Named.Exists( arrExpectArg(iii)) Then
    strResult = strResult & arrExpectArg(iii) & ": " & objArgsNamed( arrExpectArg(iii)) & " = " & Wscript.Arguments.Named.Item( arrExpectArg(iii)) & vbNewLine
  Else
    strResult = strResult & arrExpectArg(iii) & ": requested argument was not specified on the command line" & vbNewLine  
  End If
Next

WScript.Echo strResult