命令提示符和AutoIt / StdinWrite

时间:2013-10-06 08:20:56

标签: command-prompt autoit

我想学习和理解StdinWrite函数,所以编写了以下代码。

我想将数据写入提示,读取输出,再次写入数据然后再次读取输出。这是我写的代码。第一个MsgBox显示是正确的,但第二个是空白的!为什么呢?

    #include <Constants.au3>
    $pid = Run("C:\Windows\system32\cmd.exe",@SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    StdinWrite($pid,"cd ")
    StdinWrite($pid,"C:\users\test1")
    StdinWrite($pid,@CRLF)
    StdinWrite($pid)
    Local $data
    While True
        $data &= StdoutRead($pid)
        If @error Then
            MsgBox(0,"","error")
            ExitLoop
        EndIf

    WEnd
    MsgBox(0, "Debug", $data)
    StdinWrite($pid,"cd ")
    StdinWrite($pid,"C:\users\test2")
    StdinWrite($pid,@CRLF)
    StdinWrite($pid)
    $data = ""
    While True
        $data &= StdoutRead($pid)
        If @error Then
            MsgBox(0,"","error")
            ExitLoop
        EndIf

    WEnd
    MsgBox(0, "Debug", $data)
Sleep(10000)
Exit

更新 根据user2097265的评论。我已将上面的代码修改为下面的代码。我已经在下面显示了输出。而不是ConsoleWrite我在Console.au3中使用Cout。第二组漫画似乎没有奏效。

#include <Constants.au3>
#include <Console.au3>
$pid = Run("C:\Windows\system32\cmd.exe",@SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
StdinWrite($pid,"cd ")
StdinWrite($pid,"C:\users\test1")
StdinWrite($pid,@CRLF)
Local $data
Sleep(2000)
$data &= StdoutRead($pid)
;ConsoleWrite("Debug:" & $data & @LF)
Cout( $data & @LF)
$data = ""
StdinWrite($pid,"cd ")
StdinWrite($pid,"C:\users\test2")
StdinWrite($pid,@CRLF)
StdinWrite($pid,@CRLF)
StdinWrite($pid,@CRLF)
StdinWrite($pid)
$data &= StdoutRead($pid)
;ConsoleWrite("Debug:" & $data & @LF)
Cout( $data & @LF)
MsgBox(0,"","Close")

这是输出:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\system32>cd C:\users\test1

C:\Users\test1>
cd C:\users\test2

2 个答案:

答案 0 :(得分:0)

AutoIt有一个最好的帮助文件。

如果在帮助文件中检查StdinWrite,答案就在那里

If the function is called with no second argument, StdinWrite closes the stream and invalidates it for further writing.

试试这段代码:

#include <Constants.au3>
$pid = Run("C:\Windows\system32\cmd.exe",@SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
StdinWrite($pid,"cd ")
StdinWrite($pid,"C:\users\test1")
StdinWrite($pid,@CRLF)
Local $data
Sleep(2000)
$data &= StdoutRead($pid)
ConsoleWrite("Debug:" & $data & @LF)
StdinWrite($pid,"cd ")
StdinWrite($pid,"C:\users\test2")
StdinWrite($pid,@CRLF)
StdinWrite($pid)
$data &= StdoutRead($pid)
ConsoleWrite("Debug:" & $data & @LF)

答案 1 :(得分:0)

你的代码很好;在第二个Sleep之前只需要StdoutRead,给它时间返回。我没有使用<Console.au3>

要显示可能的错误,请改用$data &= StdoutRead($pid) & StderrRead($pid)