Envite
Envite

Reputation: 321

Redirection to standard output in VBS

When writing a script in sh or in cmd you can put a > at the end of a line to have the output of that line redirected to a file. If not, it is sent to the standard output.

Also, both have the echo command to produce output to the standard output (which can, in turn, be redirected too).

How to perform those two things in a VBS script?

Upvotes: 2

Views: 4790

Answers (1)

Kul-Tigin
Kul-Tigin

Reputation: 16950

Nothing different. You only need to make sure your scripts are run with the console based script host cscript.

myscript.vbs:

' WScript.Echo is a host-aware hybrid method. 
' in cscript, prints the message with final a new line feed, also accepts vary arguments (0 or more).
' in wscript, shows a message dialog window
WScript.Echo "test1", "arg2"

' WScript.Stdout.Write on the other hand is a more traditional way to write to standard output stream
' print the message as is with no new line feeds
WScript.Stdout.Write "test"
WScript.Stdout.Write "2"

Command:

cscript myscript.vbs

Output:

Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

test1 arg2
test2

There's also an option to prevent to display banner on the output.

cscript //NoLogo myscript.vbs

Output:

test1 arg2
test2

Redirection:

cscript //NoLogo myscript.vbs>output.txt

PS: cscript is the default script interpreter only on Windows Server operating systems. Otherwise the default is wscript. Therefore it is a good practice to run the scripts with a specific script host.

To change the default script host have a look at Running Your Scripts

Useful Links:


Upvotes: 2

Related Questions