函数参考


StdoutRead

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

StdoutRead ( 进程ID[, peek = false[, 二进制 = false]] )

参数

进程ID The process ID of a child process, as returned by a previous call to Run.
peek [可选参数] If true the function does not remove the read characters from the stream.
二进制 [可选参数] If true the function reads the data as binary instead of text (default is text).

返回值

成功: Returns the data read. @extended contains the number of bytes read.
失败: Sets @error to non-zero if EOF is reached, STDOUT was not redirected for the process or other error.

注意/说明

StdoutRead reads from the console standard output stream of a child process, which is normally used by console applications to write to the screen. During the call to Run for the child process you wish to read from the STD I/O parameter must have included the value of $STDOUT_CHILD (2) for this function to work properly (see the Run function).
StdoutRead does not block, it will return immediately. In order to get all data, it must be called in a loop.
Peeking on the stream does not remove the data from the buffer, however, it does return the available data as normal.
By default, data is returned in text format. By using the binary option, the data will be returned in binary format.

相关

StderrRead, StdinWrite, StdioClose, Run, RunAs

示例/演示


#include <Array.au3> ; Required for _ArrayDisplay only.
#include <Constants.au3>

; Recursively display a list of files in a directory.
Example()

Func Example()
    Local $sFilePath = @ScriptDir ; Search the current script directory.
    Local $sFilter = "*.*" ; Search for all files in the current directory. For a list of valid wildcards, search for 'Wildcards' in the Help file.
    Local $sOutput = "" ; Store the output of StdoutRead to a variable.

    ; If the file path isn't a directory then return from the 'Example' function.
    If Not StringInStr(FileGetAttrib($sFilePath), "D") Then
        Return SetError(1, 0, 0)
    EndIf

    ; Remove trailing backslashes and append a single trailing backslash.
    $sFilePath = StringRegExpReplace($sFilePath, "[\\/]+\z", "") & "\"

    #cs
        Commandline parameters for DIR:
        /B - Simple output.
        /A-D - Search for all files, minus folders.
        /S - Search within subfolders.
    #ce
    Local $iPID = Run(@ComSpec & ' /C DIR "' & $sFilePath & $sFilter & '" /B /A-D /S', $sFilePath, @SW_HIDE, $STDOUT_CHILD)
    ; If you want to search with files that contains unicode characters, then use the /U commandline parameter.

    While 1
        $sOutput &= StdoutRead($iPID) ; Read the Stdout stream of the PID returned by Run.
        If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
            ExitLoop
        EndIf
    WEnd

    ; Use StringSplit to split the output of StdoutRead to an array. All carriage returns (@CR) are stripped and @LF (line feed) is used as the delimiter.
    Local $aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@LF)), @LF)
    If @error Then
        MsgBox(4096, "", "It appears there was an error trying to find all the files in the current script directory.")
    Else
        ; Display the results.
        _ArrayDisplay($aArray)
    EndIf
EndFunc   ;==>Example