
passing Exec.StdOut back "live"?
Chopping out some garbage I didn't need, then, here's what I get...
'===
' set up for console i/o
'===
set conOut = WScript.StdOut
sub putline(str) : conOut.writeline str : end sub
set objShell = CreateObject("WScript.Shell")
strOut=""
strTargetIP="127.0.0.1"
cmdline="ping " & strTargetIP
set oExec = objShell.Exec(cmdline)
set oExecOut = oExec.StdOut
Do Until oExecOut.AtEndOfStream
strOut = trim(oExecOut.Readline)
lenOut = Len(strOut)-1
if lenOut>1 then putline Left(strOut,lenOut)
Loop
There are a variety of ways to do this but essentially you have to loop
until the WshScriptExec.Status property goes non-zero and its StdOut has
gone to end of stream...
Within the loop you *must* read the stdout stream to prevent the stdout
stream buffer from filling and blocking the Exec'd process's execution.
This isn't a critical issue with Exec'd processes that generate a small
amount of output, but it is for one that produces a large amount. I don't
really know the stdout stream buffer size is but I do know (from painful
experience ;-) that it is not unlimited.
'===
' set up for console i/o
'===
set stdin = wscript.stdin
set stdout = wscript.stdout
sub putline(str) : stdout.writeline str : end sub
function getline() : getline = stdin.readline : end function
'===
' mainline...
'===
set objShell = CreateObject("WScript.Shell")
strOut=""
strTargetIP="127.0.0.1"
cmdline="ping " & strTargetIP
set oExec = objShell.Exec(cmdline)
set oExecOut = oExec.StdOut
Do
Do Until oExecOut.AtEndOfStream
strOut = strOut & oExecOut.ReadAll
Loop
WScript.Sleep 10
Loop Until oExec.Status <> 0 and oExecOut.AtEndOfStream
putline "strOut contains:"
putline strOut
putline ""
putline "Press ENTER to continue..."
getline
wscript.quit
--
Michael Harris
Microsoft.MVP.Scripting
--
Quote:
> I want to get live "output" from StdOut if possible. I have a prototype
> which has 2 problems.
> (1) It inserts a new line after each character echoed
> (2) it never terminates
> It seems obvious to me that (2) is because I am not using AtEndOfStream
> properly to exit the loop.
> I don't know if (1) is possible - according to the WSH 5.6 docs on Echo,
> "When using CScript.exe, each item is displayed with a newline character."
> Is there an alternate way to echo the text back to a console?
> Here's my example script
> -------------------------------------------------
> set objShell = CreateObject("WScript.Shell")
> strOut=""
> strTargetIP="127.0.0.1"
> cmdline="cmd /c ping " & strTargetIP
> set oExec = objShell.Exec(cmdline)
> Do While True
> If Not oExec.StdOut.AtEndOfStream Then
> strOut = oExec.StdOut.Read(1)
> End If
> WScript.Echo strOut
> WScript.Sleep 10
> Loop
> ------------------------------------------------