Search code examples
windowscommand-linecmdshellexecute

directing output from executed process to current cmd


Can I execute a program from a JScript script in such a way that the out put of the executed program will be written to the current console?

Currently I am using Shell.Application.ShellExecute and it is opening another new console for the executed application.

The JScript in my case is a wrapper around a compiler which is executed with the ShellExecute. So what happens is that all the compiler errors are lost because they are printed in another console.


Solution

  • If all you are worried about is capturing your compiler output then you can redirect the output to a file (I suggest both stdout and stderr). Something like this:

    var shell = new ActiveXObject("WScript.Shell");
    shell.Exec('YourCommand 1>yourOutput.txt 2>&1 ')
    

    If you really want to see the ouptut in your current console window (I don't blame you), then you can use this:

    var shell = new ActiveXObject("WScript.Shell");
    WScript.StdOut.Write( shell.Exec('YourCommand 2>&1').StdOut.ReadAll() );
    

    Note - if your command has the potential for asking for input, then you must provide input via a pipe or redirection in the command string (or you could redirect input to nul). Otherwise the program will hang.