5 out of 10 rated this helpful - Rate this topic

WshScriptExec Object

Provides status information about a script run with Exec along with access to the StdIn, StdOut, and StdErr streams.

Wsh ScriptExecObject graphic

The WshScriptExec object is returned by the Exec method of the WshShell object. The Exec method returns the WshScriptExec object either once the script or program has finished executing, or before the script or program begins executing.

The following code runs calc.exe and echoes the final status to the screen.

Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")

Set oExec = WshShell.Exec("calc")

Do While oExec.Status = 0
     WScript.Sleep 100
Loop

WScript.Echo oExec.Status
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("calc");

while (oExec.Status == 0)
{
     WScript.Sleep(100);
}

WScript.Echo(oExec.Status);
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
RE: Synchronous or Asynchronous?

The description says "The WshScriptExec object is returned by the Exec method of the WshShell object. The Exec method returns the WshScriptExec object either once the script or program has finished executing, or before the script or program begins executing."

What determines whether WshScriptExec is returned immediately or after the program has finished executing?  I'd prefer asynchronous operation, which is implied by the code example with its sleep loop, but am experiencing synchronous.

If you add the .Net frame work to your Environment  variable PATH you can then use. "jsc yourCode.js" which will compile it into an .exe. "jsc /?" will give your the man page for your version of JScript. If you run the code this way you will have Synchronous runs like the code above.

problems and solutions with this way of doing JScript:

  Because you are no longer using the WScript or CScript objects to run you code you will loss the reference to "WScript.sleep(int);". I'm sure there's a .Net way to sleep but i can't find it. I created a work around by using "WshShell.run(app,windowtype, Synchronous)" to get around the lack of "WScript.sleep(int);" with a call like this, "WshShellObj.run("Sleep.exe -m 100", 6 , true);". Sleep.exe is from Windows resource tool kit. Also when compiling JScript to create the "WshShellObj" you will need to do this "var WshShellObj = new ActiveXObject("WScript.Shell");"

Hope this helps.

Synchronous or Asynchronous?

The description says "The WshScriptExec object is returned by the Exec method of the WshShell object. The Exec method returns the WshScriptExec object either once the script or program has finished executing, or before the script or program begins executing."

What determines whether WshScriptExec is returned immediately or after the program has finished executing?  I'd prefer asynchronous operation, which is implied by the code example with its sleep loop, but am experiencing synchronous.