HOME
Stephen Knight
11/01/2010
03:19 PM
Writing to STDERR and STDOUT in VBScript
Type:
VBScript
Category:
VBS
Writing to normal screen output for a console VBScript is easy using wscript.echo but to write to the STDERR stream you need to do as in the code section below. Save this as stderr.vbs and then from a cmd.exe prompt try:
cscript //nologo stderr.vbs > output.txt
The normal output will end up in output.txt and the error output to the screen
cscript //nologo stderr.vbs 2> output.txt
The error output will now end up in the log but the normal output on the screen
cscript //nologo stderr.vbs > output.txt 2>&1
Now all output will end up in the output.txt file.
This can be useful where, for instance, the output of a command needs to be piped into another for displaying, e.g
cscript //nologo somevbcode.vbs 2> errors.txt | find "whatever"
Then any errors will end up in errors.txt and the rest of the output will continue through the pipe into the FIND command and onto the console.
Code
Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
Set stderr = fso.GetStandardStream (2)
stdout.WriteLine "This will go to standard output."
stderr.WriteLine "This will go to error output."
Explanation
Examples
Attachments