littlebigman Posted Wednesday at 06:44 AM Posted Wednesday at 06:44 AM 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:".
littlebigman Posted Wednesday at 07:02 AM Author Posted Wednesday at 07:02 AM With that line, I only get one output of "dir c:\ " as expected, but still get tons of empty lines before, within, and after: While Not @error ;$line &= StdoutRead($foo) $line = StdoutRead($foo) ConsoleWrite($line & @CRLF) WEnd
Developers Solution Jos Posted Wednesday at 07:21 AM Developers Solution Posted Wednesday at 07:21 AM (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 Wednesday at 07:23 AM 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.
littlebigman Posted Wednesday at 07:36 AM Author Posted Wednesday at 07:36 AM 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.
Developers Jos Posted Wednesday at 07:45 AM Developers Posted Wednesday at 07:45 AM 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.
littlebigman Posted Wednesday at 08:18 AM Author Posted Wednesday at 08:18 AM 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 Jos Posted Wednesday at 03:11 PM Developers Posted Wednesday at 03:11 PM (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 Wednesday at 03:12 PM 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.
WildByDesign Posted Wednesday at 04:02 PM Posted Wednesday at 04:02 PM Not sure if this is helpful for your situation, but in the few programs that I’ve created that involved parsing StdOut from PowerShell and Cmd, I would often use StringStripWS. I would use various combinations of the parameters depending on the situation.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now