[ACCEPTED]-Why does this VBScript give me an error?-vbscript

Accepted answer
Score: 39

This link may help you:

http://www.tech-archive.net/Archive/Scripting/microsoft.public.scripting.vbscript/2004-07/0979.html

It appears that the 6 handle StdOut is only available when using a console 5 host (cscript.exe) and not a windowed host (wscript.exe). If you 4 want the code to work, you have to use cscript.exe to 3 run it.

The post also describes how to change 2 default behavior to run scripts with cscript 1 and not wscript.

Score: 11

As described by the article in the accepted 7 answer, my script worked when I called it 6 from the command prompt like this:

cscript test.vbs

You can 5 also change the default script host, so 4 that a call to cscript is not necessary 3 every single time. After doing that, the 2 original command works unmodified.

cscript //h:cscript //s 

You can 1 restore the original behavior with:

cscript //h:wscript //s 

Thanks!!

Score: 1

I submitted this solution in bug "cscript - print output on same line on console?" which 6 I feel is related to this issue.

I use the 5 following "log" function in my 4 JavaScript to support either wscript or 3 cscript environment. As you can see this 2 function will write to standard output only 1 if it can.

var ExampleApp = {
    // Log output to console if available.
    //      NOTE: Script file has to be executed using "cscript.exe" for this to work.
    log: function (text) {
        try {
            // Test if stdout is working.
            WScript.stdout.WriteLine(text);
            // stdout is working, reset this function to always output to stdout.
            this.log = function (text) { WScript.stdout.WriteLine(text); };
        } catch (er) {
            // stdout is not working, reset this function to do nothing.
            this.log = function () { };
        }
    },
    Main: function () {
        this.log("Hello world.");
        this.log("Life is good.");
    }
};

ExampleApp.Main();

More Related questions