2

I tried this code:

Local $foo = Run(@ComSpec & " /c dir", '', 0, 2)
Local $line
While 1
    $line = StdoutRead($foo)
    If @error Then ExitLoop
    $line = StringStripCR($line)
    If StringLen($line) > 0 Then ConsoleWrite("START" & $line & "END" & @CRLF)
WEnd

I expected to get one line at a time, but instead I get 2, 3 or 50 lines. Why does this happen?

user4157124
  • 2,809
  • 13
  • 27
  • 42
John Henckel
  • 10,274
  • 3
  • 79
  • 79

1 Answers1

3

StdoutRead() doesn't split by line feed, it just returns chunks of data. The following code parses the data into lines:

Local $foo = Run(@ComSpec & " /c dir", '', 0, 2)
Local $line
Local $done = False
Local $buffer = ''
Local $lineEnd = 0
While True
    If Not $done Then $buffer &= StdoutRead($foo)
    $done = $done Or @error
    If $done And StringLen($buffer) == 0 Then ExitLoop
    $lineEnd = StringInStr($buffer, @LF)
    ; last line may be not LF terminated:
    If $done And $lineEnd == 0 Then $lineEnd = StringLen($buffer)
    If $lineEnd > 0 Then
        ; grab the line from the front of the buffer:
        $line = StringLeft($buffer, $lineEnd)
        $buffer = StringMid($buffer, $lineEnd + 1)

        ConsoleWrite("START" & $line & "END" & @CRLF)
    EndIf
WEnd
John Henckel
  • 10,274
  • 3
  • 79
  • 79