Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vbscript

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?

like image 557
Landister Avatar asked Jan 19 '23 02:01

Landister


1 Answers

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.

like image 75
Kev Avatar answered May 14 '23 00:05

Kev