【问题标题】:Run child process and capture output one line at a time运行子进程并一次捕获一行输出
【发布时间】:2013-07-11 06:37:59
【问题描述】:

我试过这段代码:

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

我希望一次得到一条线,但我得到了 2、3 或 50 条线。为什么会这样?

【问题讨论】:

标签: stdout autoit


【解决方案1】:

StdoutRead() 不按换行符拆分,它只是返回数据块。以下代码将数据解析为行:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-20
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 2016-07-21
    • 1970-01-01
    相关资源
    最近更新 更多