JScript: how to run external command and get output?


I’m running my JScript file using cscript.exe.
In the script I need to call an external console command and get the output.


var oShell = WScript.CreateObject("WScript.Shell");
var oExec = oShell.Exec('cmd /c dir');
WScript.Echo("Status "+oExec.Status);
WScript.Echo("ProcessID "+oExec.ProcessID);
WScript.Echo("ExitCode "+oExec.ExitCode);


var oShell = WScript.CreateObject("WScript.Shell");
var ret = oShell.Run('cmd /c dir', 1 /* SW_SHOWNORMAL */, true /* bWaitOnReturn */);
WScript.Echo("ret " + ret);

but no luck: the command runs (most likely) without errors but I have no output.
Please note ‘cmd /c dir’ here is just example to make sure I get any output at all.

So, how should I do it?

I tried to convert this https://stackoverflow.com/a/6073170/1013183 to JScript but no luck too:

var oShell = WScript.CreateObject("WScript.Shell");
var oExec = oShell.Exec('cmd /c dir');
var strOutput = oExec.StdOut.ReadAll;
WScript.Echo("StdOut "+strOutput);

var strOutput = oExec.StdErr.ReadAll;
WScript.Echo("StdErr "+strOutput);

The error is Microsoft JScript runtime error: Object doesn't support this property or method at var strOutput = oExec.StdOut.ReadAll; line


var strOutput = oExec.StdOut.ReadAll();

In Javascript it is a call to a function and MUST include the parenthesis

Answered By – MC ND

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published