Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WshScriptExec.StdOut flakiness

206 views
Skip to first unread message

Tom Briden

unread,
Oct 9, 2002, 4:36:05 PM10/9/02
to
Hi,

I've been tasked with trying to convert some rather involved .bat scripts
into VBS (not my choice, so don't ask why) for an automated code building
system. Some of the stuff requires standard command line calls to
executables and returning the exit code, StdOut, and StdErr. The WSH docs
show quite nicely how one should be able to do this easily with the
WScript.Shell Exec function and the returned WshScriptExec object which
gives access to exit code, StdIn, StdOut, and StdErr. Of course, as usual
with any MS technology, it never seems to works as advertised.

I am using the code shown at the bottom of the page, more or less lifted
verbatim from the docs. This works fine for anything that runs quickly and
returns trivial amounts of output. However, I need to be able to capture
output from PVCS commands and compilers, which can be quite lengthy, and
these commands invariably just simply hang in mid-operation for no apparant
reason. It almost seems as if too much StdOut is blowing out the memory
capacity of the WshScriptExec object and halting the process, because if I
call the Exec function without return the WshScriptExec object, the command
finishes normally. However, without access to StdOut, etc., this is
worthless to me.

I read one post back on the thread which spoke of problems when accessing
the properties of the WshScriptExec object too often during execution and
suggested caching local object references to StdOut and StdErr.
Unfortunately, the only property I'm referencing during execution is Status
(just like in the documentation samples), and I get an error if I cache it
in a local variable.

The documentation provides no help or detailed technical information to even
give me a clue here. I'm hoping someone out there has had to deal with this
and has a solution.

Thanks,

Tom Briden


----------------------------------------------------------------------------
-----------
Function ExecuteShellCommand( sCommand, sReturnCode, sStdOut, sStdErr )

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

Set oExec = WshShell.Exec(sCommand)

Do While oExec.Status= 0
WScript.Sleep 100

Loop


sReturnCode = "ExitCode = " + oExec.ExitCode

If Not oExec.StdOut.AtEndOfStream Then
sStdOut = "STDOUT: " + oExec.StdOut.ReadAll
Exit Function
End If

If Not oExec.StdErr.AtEndOfStream Then
sStdErr = "STDERR: " + oExec.StdErr.ReadAll
Exit Function
End If

End Function


Michael Harris (MVP)

unread,
Oct 9, 2002, 7:42:34 PM10/9/02
to
The problem is that the standard streams are buffers. They can become full and block the process that is writing to them if the process that is supposed to be reading from them doesn't...

Below is a generalized template I start with for WshShell.Exec stuff. I don't service the StdErr stream in the loop because it generally won't fill and block. But if that is a possibility in your case, you can easily adapt the logic to accumulate both StdOut and StdErr and wait for both to be AtEndOfStream when the Status goes non-zero...

Using a Dictionary to accumulate output is *far* faster that repeated string concatenation but (surprisingly) slower that ReDim'ing a dynamic array one element at a time (with Preserve of course). I use the dictionary method because you don't need to do anything explicit to allow it to grow in size. If I *really* needed performance I'd use the dynamic array method.


sCmd = ""

set shell = createobject("wscript.shell")
Set d = CreateObject("Scripting.Dictionary")

set wsx = shell.exec(sCmd)
set wsxOut = wsx.stdout

do: wscript.sleep 10
do until wsxOut.atendofstream
d(d.count) = wsxOut.readline
loop
loop until wsx.status <> 0 and wsxOut.atendofstream

arLines = d.items()


--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US


Tom Briden

unread,
Oct 10, 2002, 10:37:37 AM10/10/02
to
Thanks, that was what I needed.

MS really needs to have some more in depth documentation for this stuff.
Who would know ReadLine actually empties the buffer?

"Michael Harris (MVP)" <mik...@mvps.org> wrote in message
news:#LBzf1#bCHA.2464@tkmsftngp11...

anthonyb...@gmail.com

unread,
Dec 17, 2018, 6:48:49 PM12/17/18
to
As I ponder a life many would say has been mis-spent, I realize all along it wasn't me but the WshScriptExec.StdOut that has kept me out of contention for the directorship of the Large Hadron Collider. Fuck. This one got posted way too late for me Bubs.

0 new messages