Other Languages - WSH: WshShell.Exec() - Correct usage oExec.Status & output capturing

Asked By zero flag on 19-Dec-11 04:20 AM
Among other features, Wsh.Shell.Exec() method allows doing following two things:
(1)  Performing a synchronous command execution - meaning providing way of waiting until
      the cmd's completion by loop-checking oExec.Status value. A typical code looks like this:

      Set objShell = WScript.CreateObject("WScript.Shell")
      Set objExec = objShell.Exec(some_cmd)
      Do While objExec.Status = 0
               WScript.Sleep 100
      Loop

(2)  Capturing output of the cmd that writes to STDOUT (and STDERR), etc. Code looks like:

     Set objExec = objShell.Exec(some_cmd)
     Do While Not objExec.StdOut.AtEndOfStream
        strText = objExec.StdOut.ReadLine()

        ' More process here if any....
     Loop

My question is:  what is a correct way, or how should I write the code that joins both above
at the same time  - both "checking Status" and "capturing output"?  Or is it a need to use both?
namely, I doubt if it is necessary to use both at the same time  -  when reading of output reaches
end (above (2)), the cmd execution itself naturally comes to an end (above (1)), too, so there is
no need to further check Status, isn't the case?

Thanks for advice.
Sri K replied to zero flag on 27-Dec-11 09:01 AM

Yes, the Exec function seems to be broken when it comes to terminal output.

I have been using a similar function function ConsumeStd(e) {WScript.StdOut.Write(e.StdOut.ReadAll());WScript.StdErr.Write(e.StdErr.ReadAll());} that I call in a loop similar to yours. Not sure if checking for EOF and reading line by line is better or worse.

zero flag replied to Sri K on 27-Dec-11 08:06 PM
Tosh,

Thank you for replying, a good reference I will try in my code.
Sri K replied to zero flag on 01-Jan-12 03:17 AM
you are welcome.

happy new year