Click to See Complete Forum and Search --> : vbs: Wscript.Shell - Reading from StdOut, writing to Text Stream


0x0000ff
September 21st, 2005, 08:18 PM
I'm writing a script that needs to be able to launch a process, read and write to/from stdout/in, and store the data sent through these streams into a custom text stream object.

Here is a very simplified version of what I want to do, taken from an example on MSDN:


Dim WshShell, oExec, input
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("test.bat")
input = ""

Do While True

If Not oExec.StdOut.AtEndOfStream Then
input = input & oExec.StdOut.Read(1)
If InStr(input, "Press any key") <> 0 Then Exit Do
End If
WScript.Sleep 100
Loop

oExec.StdIn.Write VbCrLf

Do While oExec.Status <> 1
WScript.Sleep 100
Loop



Now the problem with this code is, as you can see; it's exiting the DO loop when it reads "Press any key"

Now if you've ever run pause.com before you know the actual output is "Press any key to continue . . . "

So this code stops reading from StdOut before it's captured all the data I want to read from it.

PROBLEM: If this loop reads EVERYTHING inside StdOut, it will hang at the "AtEndOfStream" check. I need to work out a way to make it exit the loop when it's read everything, or prevent it from trying to perform operations on StdOut when there's nothing there to read.

YES, oExec will be waiting for a keypress, but if I can't read everything sent to StdOut then it's entirely useless to me.

Does anyone have any ideas?