Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text
- From: don.jscript.usenet@xxxxxxxxx
- Date: Tue, 29 Jan 2008 07:13:32 -0800 (PST)
Unfortunately, AtEndOfStream will still wait. I will not be able to
terminate the app if it takes too long without returning any output.
I love your use of cmd.exe, feeding in commands by writing to stdin.
You can change a command to execute based on the results of the
previous command.
On Jan 27, 6:28 pm, badbadsh...@xxxxxxxxxxxxxx wrote:
Instead of exec'ing the thing you want to execute you can exec another
cmd.exe.
You can then fire in whatever commands you actually want to execute
into oExec.StdIn.
This lets you send in your desired command but also markers, such as
'echo marker1'
which you can search for in the output stream so you know when to stop
reading.
It isn't pretty but it works. Plus you can use the same oExec for many
commands
which is more efficient if you want to exec a lot.
Here is some sample code, it is vbscript but you can use the same idea
in jscript:
dim WshShell, oExec, iExitCodeNum, iExitCode, sOut
set WshShell = WScript.CreateObject("WScript.Shell")
set oExec = WshShell.Exec("%comspec% /d/q")
iExitCodeNum = 1
oExec.StdIn.WriteLine("prompt TIMESTAMP $D $T$_")
' skip over any blurb before we start our commands
iExitCode = getExitCode(sOut)
function getExitCode(byRef sOut)
getExitCode = -1
dim sExitCodeName, sOutLine
' the Jkdfhudi just makes accidental matches v.unlikely
sExitCodeName = "exitCodeJkdfhudi" & iExitCodeNum
oExec.StdIn.WriteLine("echo " & sExitCodeName & "=%ERRORLEVEL%")
dim outRE, outM
set outRE = new RegExp
outRE.Global = false
outRE.ignoreCase = false
outRE.Pattern = sExitCodeName & "=(\d+)"
sOut = ""
do while not oExec.StdOut.AtEndOfStream
sOutLine = oExec.StdOut.ReadLine
set outM = outRE.Execute(sOutLine)
if outM.count = 1 then
getExitCode = CLng(outM(0).SubMatches(0))
exit do
else
sOut = sOut & sOutLine & vbcrlf
end if
loop
iExitCodeNum = iExitCodeNum + 1
end function
sOut = ""
iExitCode = -1
on error resume next
oExec.StdIn.WriteLine("cscript /nologo output.js 2>&1")
iExitCode = getExitCode(sOut)
on error goto 0
if iExitCode = -1 then
' failed - exec'ed cmd shell may have been closed
...
end if
WScript.Echo(sOut)
On Jan 11, 3:21 pm, don.jscript.use...@xxxxxxxxx wrote:
I want to execute an app, process its output, but terminate the app if
it takes too long. I thought using WScript.Shell.Exec() would be the
ticket. However, I'm finding that AtEndOfStream waits until text
appears in the executed app's stdout, or the app terminates. Read()
works the same, and ReadAll() waits for the app to terminate, though I
expect that behavior.
Is there a workaround or alternative? As it stands, I can either
process the app's output as it comes, or terminate the app if it takes
too long, but not both.
Here's a simple example. output.js pauses before and after outputting
a single character. process.js executes output.js, checks
AtEndOfStream, Read()'s the single character, and checks AtEndOfStream
again. You'll notice the pause before each AtEndOfStream is output --
AtEndOfStream waits for the single character, and then waits for the
app to terminate.
process.js
========================================
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("cscript /nologo output.js");
var ch;
var aeos;
WScript.StdOut.WriteLine("starting...");
aeos = oExec.StdOut.AtEndOfStream;
WScript.StdOut.WriteLine("AtEndOfStream=" + aeos);
ch = oExec.StdOut.Read(1);
WScript.StdOut.WriteLine("Read(1)=" + ch);
aeos = oExec.StdOut.AtEndOfStream;
WScript.StdOut.WriteLine("AtEndOfStream=" + aeos);
output.js
========================================
WScript.Sleep(3000);
WScript.StdOut.Write("a");
WScript.Sleep(3000);
.
- References:
- WScript.Shell.Exec().StdOut.AtEndOfStream waits for text
- From: don . jscript . usenet
- Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text
- From: badbadshark
- WScript.Shell.Exec().StdOut.AtEndOfStream waits for text
- Prev by Date: Re: Replace last occurrence of substring
- Next by Date: Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text - solution
- Previous by thread: Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text
- Next by thread: Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text - solution
- Index(es):