Jump to content

CMD Prompt StdoutRead issues


bran
 Share

Recommended Posts

Hello,

I am having issues using StdoutRead() to read from the CMD prompt window.

I started with the StdoutRead example on the function reference page. This example does not work for me. So I have spent the last 5 hours searching through posts on this forum and Google trying different variations of code, and no success. I know that this will be an easy question for most of you Autoit experts reading this, and I will really appreciate your help!

Here is the code I have modified. I am trying to read the stream that is being sent to the WSUS Offline Updater CMD prompt window. I have opened the CMD prompt as a child as illustrated in the example on the function reference page, and it doesn't work.

#include <Constants.au3>

Local $OutPut = Run(@ComSpec & " /c G:\wsusoffline\client\Update.cmd", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = StdoutRead($OutPut)
    If @error Then ExitLoop
    MsgBox(0, "STDOUT", $line)
Wend

While 1
    $line = StderrRead($OutPut)
    If @error Then ExitLoop
    MsgBox(0, "STDERR", $line)
Wend

One more question, am I suposed to see the CMD window that opens from the Run() command since @SW_HIDE is used in the show_flag?

Thanks in advance for your help!

Link to comment
Share on other sites

Does Update.cmd show anything in a normal command window when executed manually?

Also, here's something I put together a few weeks ago ... perhaps it might help you.

Local $sPath = FileOpenDialog('Select File', '', 'All Files (*.*)', 1)
If Not $sPath Then Exit
Local $rtn = TestFile($sPath)
MsgBox(0, '', $rtn)

Func TestFile($sPath)
    Local $eMsg, $stdout
    Local $pid = Run('cmd.exe /c "' & $sPath & '"', '', @SW_HIDE, 6)
    If Not $pid Then Return SetError(-1, 0, 'Failed to run')
    ;
    While 1
        $eMsg &= StderrRead($pid, 0, 0)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    If $eMsg Then Return SetError(-2, 0, $eMsg)
    ;
    While 1
        $stdout &= StdoutRead($pid, 0, 0)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    ;
    If Not $stdout Then Return SetError(-3, 0, 'Nothing to read')
    Return SetError(0, 0, $stdout)
EndFunc

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Does Update.cmd show anything in a normal command window when executed manually?

Also, here's something I put together a few weeks ago ... perhaps it might help you.

Local $sPath = FileOpenDialog('Select File', '', 'All Files (*.*)', 1)
If Not $sPath Then Exit
Local $rtn = TestFile($sPath)
MsgBox(0, '', $rtn)

Func TestFile($sPath)
    Local $eMsg, $stdout
    Local $pid = Run('cmd.exe /c "' & $sPath & '"', '', @SW_HIDE, 6)
    If Not $pid Then Return SetError(-1, 0, 'Failed to run')
    ;
    While 1
        $eMsg &= StderrRead($pid, 0, 0)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    If $eMsg Then Return SetError(-2, 0, $eMsg)
    ;
    While 1
        $stdout &= StdoutRead($pid, 0, 0)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    ;
    If Not $stdout Then Return SetError(-3, 0, 'Nothing to read')
    Return SetError(0, 0, $stdout)
EndFunc

Greats! what directive you use to send a command to a running cmd ? i want to learn :)
Link to comment
Share on other sites

Ripdad,

Thank you for your help. Very impressive snippet of code, especially being able to browse for the file to run.

I ran your code and got the the "Nothing to read" Msgbox. So I know now that it was not my code that wasn't working, rather another issue is stopping me.

Yes, there is output on the cmd window when run manually. Also when run from the Run() function in Autoit there is output in the cmd window, which if I read up correctly on the StdoutRead() function that means that the stream is not being intercepted, correct?

Hopefully with everyone's help we can get this figured out, I have spent alot of time trying to get this far. Thanks again for your help!

Edited by bran
Link to comment
Share on other sites

Thanks, but nothing impressive about it - just needed it to run some output test on some exe's.

I can think of 2 possibilities about your problem.

1) Change the working directory to "G:\wsusoffline\client"

Local $workingdir = "G:\wsusoffline\client"

Local $pid = Run('cmd.exe /c "' & $sPath & '"', $workingdir, @SW_HIDE, 6)

2) You only get a StdoutRead if the program writes to the command window.

Now, does Update.cmd write to the window or does another program write it from Update.cmd?

Most .cmd files are nothing more than batch files (.bat)

If the batch file only executes update programs and not write to the window ... I think you might see my point?

If update.exe spawns another window, you might have to skip the batch file and get it from update.exe(example)

instead. (I hope that makes sense - I haven't had my 1st cup of coffee yet)

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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...