开发者

How to output a list of arguments passed to a VBScript script?

I have the following VBScript code:

Dim returnVal
returnVal = "You did not pass me 4 arguments"

args = WScript.Arguments.Count

If args = 4 The开发者_StackOverflown
    returnVal = "The arguements you passed me are " & WScript.Arguments.Item(0) & "  "  & WScript.Arguments.Item(1) & "  "  & WScript.Arguments.Item(2) & "  "  & WScript.Arguments.Item(3) 
end if

All I want is the ability to print "returnVal" so that if I typed:

test.vbs 1 2 3 4

It would return:

The arguments you passed me are 1 2 3 4

How can I do this?


To output to the command console window you can do this using:

WScript.Echo returnVal 

or

WScript.StdOut.WriteLine returnVal 

But you must use the CScript host for this to work, for example:

cscript.exe myscript.vbs

WScript is the GUI host and so has no knowledge of the standard input/output/error/aux streams. Trying to do WScript.StdOut.WriteLine will result in the following error dialogue:

---------------------------
Windows Script Host
---------------------------
Script: d:\myscript.vbs
Line:   12
Char:   1
Error:  The handle is invalid. 
Code:   80070006
Source:     (null)

---------------------------
OK   
---------------------------

In a CScript.exe script you can still pop up GUI message dialogues using:

Msgbox "Hello World!" 

Using WScript.Echo in a WScript host will display the message in a popup dialogue instead of printing to the command line window.

For more information see:

Write Method (Windows Script Host)

For more information on the differences between WScript and CScript and how to switch between them:

Sesame Script Stop and Go (MS TechNet)

The difference between Cscript and Wscript is that Cscript is the command-line version of the script host and Wscript is the graphical version. This difference isn’t really noticeable unless your script uses the Wscript.Echo command.


If you're not after the Message Box suggested by @heximal, you should use StdOut:

WScript.StdOut.Write(returnVal)

Important: This requires CScript to be the host executable.

To change the default script host, use

cscript //h:cscript //s
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜