4

I have the following VBScript code:

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

args = WScript.Arguments.Count

If args = 4 Then
    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?

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Dang - 3 secs ... and you included cscript usage.
For my sins, I worked in a bank and used to look after a 30000 line build system written entirely in vbscript. This experience is burned irreversibly into my noggin :)
I did not figure it was a hard problem but I am new to scripting. Thanks man :)
@Kev: +1 for this excellent summary of w/cscript output. May I ask you to take out the misleading 'pass me per value' () from your example sub calls, however?
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.