StdoutRead
读取运行的子进程 STDOUT 流.
StdoutRead ( process_id [, peek = False [, binary = False]] )
参数
process_id
|
子进程 ID, 由先前调用的 Run 返回.
|
peek
|
[可选] 如为 true, 函数不删除从流中读取字符.
|
binary
|
[可选] 如为 true, 函数读取二进制数据, 而非文本(默认为文本).
|
返回值
成功:
|
返回读取的数据. @extended 包含读取的字节数.
|
失败:
|
@error 被设置为非 0, 到达 EOF(文件尾), STDOUT 不再为进程或其它错误重定向.
|
备注
StdoutRead 从控制台读取子进程标准输出流数据, 通常使用控制台程序写入到屏幕.
对子进程调用 Run 期间, 要读取标准 I/O 值, 参数必须包括 $STDOUT_CHILD (2), 函数才能正常工作 (见 Run 函数).
StdoutRead 不被阻塞时, 将立刻返回. 要获取所有数据, 它必须在循环中调用.
在流上"取数"不会把数据从缓冲器删除, 且能返回正常可用的数据.
默认数据以文本格式返回. 如使用"二进制"选项, 数据将以二进制格式返回.
相关
Run, RunAs, StderrRead, StdinWrite, StdioClose
函数示例
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3> ; Required for _ArrayDisplay only.
; 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.
; 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.
; Wait until the process has closed using the PID returned by Run.
ProcessWaitClose($iPID)
; Read the Stdout stream of the PID returned by Run. This can also be done in a while loop. Look at the example for StderrRead.
Local $sOutput = StdoutRead($iPID)
; Use StringSplit to split the output of StdoutRead to an array. All carriage returns (@CRLF) are stripped and @CRLF (line feed) is used as the delimiter.
Local $aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)
If @error Then
MsgBox($MB_SYSTEMMODAL, "", "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
----------------------------------------
|