1

I wanted to study and understand the StdinWrite function and so wrote the following piece of code.

I want to write data to the prompt, read the output, write the data again and then read the output again. Here is the code I have written. The first MsgBox display is correct, but the second is blank! Why?

    #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

Update: Based on the comments by user2097265. I have modified the above code to the one below. I have shown the output below. Instead of ConsoleWrite I am using Cout in Console.au3. The second set of commans don't appear to have worked.

#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")

Here is the output:

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
0

2 Answers 2

0

Your code is fine; just needs Sleep before the second StdoutRead as well, to give it time to return. I didn't use <Console.au3> though.

To display possible errors use $data &= StdoutRead($pid) & StderrRead($pid) instead.

0

AutoIt has one of the best Help files out there.

If you check StdinWrite in the Help file, the answer is there

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

Try this code:

#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)
5
  • I did what you said. I do not see anything. But I think I don't know how to work with ConsoleWrite. How do I see the output of ConsoleWrite?
    – Nazir
    Oct 7, 2013 at 1:39
  • I did somemore research on ConsoleWrite - I believe if I compile with CUI option I should be able to see the output, but where is this option in SciTE?
    – Nazir
    Oct 7, 2013 at 3:02
  • I have updated my query based on your answer. I have also posted the output. I am not getting the output what I needed.
    – Nazir
    Oct 7, 2013 at 5:41
  • Please use @Milos when u ask me so I will get notified. You can see Console Write data in the bottom of scite and its live. Open your script in scite,press F5 and watch the bottom of it. Just add another Sleep(2000) before last StdoutRead($pid) Cheers mate
    – Milos
    Oct 7, 2013 at 19:32
  • When you don't see the output area in Scite, you need to press {F8} and it will appear below your script. But it should appear automatically when you run your script from within Scite with {F5}. And yeah, as I already posted, @Milos is correct. You need another Sleep to wait for the output to be completed. Or programatically continue on reading until you get a result different to "".
    – Samoth
    Oct 8, 2013 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.