Jump to content

get cmd output


Recommended Posts

Dear All.

I want to use Autoit get cmd output.

Here is my code, but no ouput, 

 

Please help to correct where is error, thanks!

 

  #include <Constants.au3>


    Local $hWnd = Winactivate("Administrator: Command Prompt")

    ; Display a list of Notepad processes returned by ProcessList.
    Local $aProcessList = ProcessList("cmd.exe")
    For $i = 1 To $aProcessList[0][0]
        MsgBox(0, "", $aProcessList[$i][0] & @CRLF & "PID: " & $aProcessList[$i][1])
  ;$iPID = $aProcessList[$i][1]

      Local $content = StdoutRead($aProcessList[$i][1])
      MsgBox(0, "讀取到的 STDOUT 為:", $content)

      FileWrite(@DesktopDir &"\Test.txt", $content &@CRLF)
  Next

Link to comment
Share on other sites

I just ran this and it did what I would expect it to:

I had two cmd.exe processes running (according to task manager), your code went through the For loop twice, the first MsgBox reported the process name and PID, the second was empty: You are using StdoutRead, which gives you the STDOUT stream from a previously run process, but the ProcessList function call is not a child process and you do not create any other child processes, hence STDOUT is empty.

Perhaps if you explain in more detail what you want your code to do we can suggest an alternative approach?

Edited by SlackerAl

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

Hi,

Try checking this link if that suffice your expectation. And check with help file about Run and StdoutRead as what SlackerAl means.

 

 

KS15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Let me see if I have it clearly.

You want to get the output from a cmd.exe with Autoit? is that right? Like:

Run(@ComSpec & " /c dir > output.txt", @ScriptDir)

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...