Jump to content

[Run] Right way to loop-read StdOut?


Go to solution Solved by Jos,

Recommended Posts

Posted

Hello,

I need to run an external program, read its stdout until there's nothing left to read, display whatever stdout + stderr finally contain, and end.

For some reason, the following code outputs tons of repeats. I expected 1) each call to StdoutRead() to empty the buffer, and 2) no repeats.

What am I doing wrong?

Thank you.

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <Clipboard.au3>
#include <File.au3>
#include <String.au3>

$sCMD = @ComSpec & " /c DIR"

Local $line = ""
Local $foo = Run($sCMD , "C:\", @SW_SHOW, BitOR($STDERR_CHILD, $STDOUT_CHILD))
While Not @error
    $line &= StdoutRead($foo)
    ConsoleWrite($line & @CRLF)
WEnd
ConsoleWrite("Final STDOUT read:" & $line & @CRLF)

ConsoleWrite("Stderr Read:" & StderrRead($foo) & @CRLF)

;=> Tons of empty lines, followed by several repeats of the $sCMD command output, followed by a single occurence of "STDOUT read:".
  • Developers
  • Solution
Posted (edited)

That is what your script tells it to do, to repeat the same thing over and over again and also add CRLF when nothing is received.
Try:
 

#include <AutoItConstants.au3>
$sCMD = @ComSpec & " /c DIR"
Local $tline = ""
Local $foo = Run($sCMD , "C:\", @SW_SHOW, BitOR($STDERR_CHILD, $STDOUT_CHILD))
While Not @error
    $cline = StdoutRead($foo)
    if $cline <> "" Then
        $tline &= $cline
        ConsoleWrite($cline)
    EndIf
WEnd
ConsoleWrite("Final STDOUT read:" & $tline & @CRLF)

ConsoleWrite("Stderr Read:" & StderrRead($foo) & @CRLF)


 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

  • Developers
Posted
7 minutes ago, littlebigman said:

Thanks. It's code I found through Google. I expected "While Not @error" to only display data it actually received, and exit when it's done.

It really helps when you also try to understand code before running it. 😉 
The @error test is all about "as long as there is a valid RUN() session" keep this loop running.

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted

I don't see how a user could guess that @error could mean something else than "exit if there's nothing left to read" or that the loop would print empty lines not present in the output.

Nothing to do with intelligence.

But thanks anyway.

  • Developers
Posted (edited)
6 hours ago, littlebigman said:

I don't see how a user could guess that @error could mean something else than "exit if there's nothing left to read" or that the loop would print empty lines not present in the output.

Nothing to do with intelligence.

But thanks anyway.

Excuse me when you find my previous reply offensive, which isn't intended, but since AI  the RTFM has been buried somewhere deep! 😉 
anyway, F5/help would have told you that. 🙂 
 

Quote

StdoutRead

Reads from the STDOUT stream of a previously run child process.

StdoutRead ( process_id [, peek = False [, binary = False]] )

-snip-

Return Value

Success: the data read. @extended contains the number of bytes read.
Failure: sets the @error flag to non-zero if EOF is reached, STDOUT was not redirected for the process or other error.

 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...