KaFu Posted January 14, 2009 Share Posted January 14, 2009 (edited) Just a small (but imho useful) example... #include <Constants.au3> ; Example 1 $foo = Run(@ComSpec, "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD) StdinWrite($foo, "dir /b c:" & @CRLF) StdinWrite($foo, "echo. Echo finished" & @CRLF) StdinWrite($foo, "exit" & @CRLF); send exit command to trigger stdout $line = "" While 1 $line &= StdoutRead($foo) If @error Then ExitLoop Wend MsgBox(0,"","Finished" & @crlf & @crlf & @crlf & $line) ; Example 2 ; Command1 && Command2; Run second command if first command executed successfully ; Command1 || Command2; Run second command if first command executed unsuccessfully $foo = Run(@ComSpec & " /k dir /b c: && Echo. && Echo finished && Exit", 'c:\', @SW_HIDE, $STDOUT_CHILD) $line = "" While 1 $line &= StdoutRead($foo) If @error Then ExitLoop Wend MsgBox(0,"","Finished" & @crlf & @crlf & @crlf & $line) Best Regards Edited January 27, 2009 by KaFu OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
BinaryBrother Posted June 30, 2009 Share Posted June 30, 2009 Too bad no-one picked up on this (Or failed to comment), real handy! Thanks... SIGNATURE_0X800007D NOT FOUND Link to comment Share on other sites More sharing options...
KaFu Posted June 30, 2009 Author Share Posted June 30, 2009 Glad you like it . Found this one while researching how to extract unicode from cmd.exe (chcp brings you close to certain things, but the rest in vain). OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
WhyTea Posted September 10, 2009 Share Posted September 10, 2009 Just a small (but imho useful) example... #include <Constants.au3> ; Example 1 $foo = Run(@ComSpec, "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD) StdinWrite($foo, "dir /b c:" & @CRLF) StdinWrite($foo, "echo. Echo finished" & @CRLF) StdinWrite($foo, "exit" & @CRLF); send exit command to trigger stdout $line = "" While 1 $line &= StdoutRead($foo) If @error Then ExitLoop Wend MsgBox(0,"","Finished" & @crlf & @crlf & @crlf & $line) ; Example 2 ; Command1 && Command2; Run second command if first command executed successfully ; Command1 || Command2; Run second command if first command executed unsuccessfully $foo = Run(@ComSpec & " /k dir /b c: && Echo. && Echo finished && Exit", 'c:\', @SW_HIDE, $STDOUT_CHILD) $line = "" While 1 $line &= StdoutRead($foo) If @error Then ExitLoop Wend MsgBox(0,"","Finished" & @crlf & @crlf & @crlf & $line) Best Regards Thanks for the code. I managed to modify it to drive some command line programs. However, nothing could be read from a command line program's STDOUT, which was written in C++ and compiled with MS VC++ (someone else's program). In order to see where the problem could be, I wrote a simple command line program in C and compiled with LCC - I managed to read data from its STDOUT. I couldn't work out where the problem is. Don't cout and printf go to STDOUT? Is there anything else I can try? /Why Tea Link to comment Share on other sites More sharing options...
whim Posted September 10, 2009 Share Posted September 10, 2009 Thanks for the code. I managed to modifyit to drive some command line programs.However, nothing could be read from acommand line program's STDOUT, which waswritten in C++ and compiled with MS VC++(someone else's program). In order to see where the problem could be, I wrote a simplecommand line program in C and compiledwith LCC - I managed to read data from itsSTDOUT. I couldn't work out where the problem is. Don't cout and printf go toSTDOUT? Is there anything else I can try?/Why Teahave you tried redirecting STDERR to STDOUT ?whim Link to comment Share on other sites More sharing options...
WhyTea Posted September 10, 2009 Share Posted September 10, 2009 have you tried redirecting STDERR to STDOUT ?whimyes I did. I used $STDERR_MERGED. Link to comment Share on other sites More sharing options...
KaFu Posted September 10, 2009 Author Share Posted September 10, 2009 Hmmm, sorry, don't know about C. Try if it works from command-shell cmd.exe manually. If it pipe to cmd.exe I don't see why it shouldn't be captured. Otherwise maybe refer to the developer chat to ask C questions . OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
WhyTea Posted September 10, 2009 Share Posted September 10, 2009 Hmmm, sorry, don't know about C. Try if it works from command-shell cmd.exe manually. If it pipe to cmd.exe I don't see why it shouldn't be captured. Otherwise maybe refer to the developer chat to ask C questions .I think it most likely has nothing to do with C or C++. The following code works:#include <Constants.au3>Local $my_prog = "c:\somewhere\bin\my_prog.exe"Local $pid = Run(@ComSpec & " /k " & $my_prog)Sleep(100)Send ("C")Sleep(50)Send ("MS")Sleep(100)Send ("0{ENTER}1{ENTER}1{ENTER}2{Enter}")But I when I tried to hook up the STDIO anduse the StdoutRead()/StdinWrite() so that Ican check the output strings, outputs tothe screen didn't show up anywhere. I'm very new to Autoit, I basically used yourcode as samples to my code. The following codefailed:Local $pid = Run(@ComSpec, "c:\somewhere\bin\", @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)StdinWrite ($pid, "my_prog.exe" & @CRLF) Sleep(150)StdinWrite ($pid, "C") ; no CR neededSleep(50)StdinWrite ($pid, "M") ; no CR neededSleep(50)StdinWrite ($pid, "S") ; no CR neededSleep(50)StdinWrite ($pid, "0" & @CRLF)StdinWrite ($pid, "1" & @CRLF)StdinWrite ($pid, "1" & @CRLF)StdinWrite ($pid, "2" & @CRLF)Local $cnt = 1Local $data = ""While True $data &= StdoutRead ($pid) ConsoleWrite($data) If @error Then ExitLoop If StringInStr ($data, "<009>") <> 0 Then $result = 1 MsgBox (0,"Test Passed!", $data) ExitLoop EndIf Sleep(30) $cnt = $cnt + 1 If $cnt == 100 Then ExitLoop EndIfWEndIf $result == 0 Then MsgBox (0,"Test Failed!", $data)EndIf Do you see any fault in the code above?/Why Tea Link to comment Share on other sites More sharing options...
KaFu Posted September 10, 2009 Author Share Posted September 10, 2009 As far as I see the problem is with triggering the output to the console... in my example above the line StdinWrite($foo, "exit" & @CRLF); send exit command to @ComSpec to trigger stdout! does this. But I guess you also need a command for your app to close first (to make ComSpec accept "exit")? Try something like StdinWrite($pid, "Q" & @CRLF) ; send some quit command to your app to exit back to console! or... even processclose($pid) might work ...but I'm not sure about that. #include <Constants.au3> Local $pid = Run(@ComSpec, "c:\", @SW_SHOW, $STDIN_CHILD + $STDERR_MERGED) StdinWrite ($pid, "my_prog.exe" & @CRLF) Sleep(150) StdinWrite($pid, "C") ; no CR needed Sleep(50) StdinWrite($pid, "M") ; no CR needed Sleep(50) StdinWrite($pid, "S") ; no CR needed Sleep(50) StdinWrite($pid, "0" & @CRLF) StdinWrite($pid, "1" & @CRLF) StdinWrite($pid, "1" & @CRLF) StdinWrite($pid, "2" & @CRLF) StdinWrite($pid, "Q" & @CRLF) ; send some quit command to your app to exit back to console! StdinWrite($foo, "exit" & @CRLF); send exit command to @ComSpec to trigger stdout! While 1 $data &= StdoutRead($pid) If @error Then ExitLoop Wend ConsoleWrite($data) If StringInStr($data, "<009>") <> 0 Then MsgBox(0, "Test Passed!", $data) Else MsgBox(0, "Test Failed!", $data) EndIf OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
jvanegmond Posted September 10, 2009 Share Posted September 10, 2009 I think it most likely has nothing to do with C or C++. The following code works:#include <Constants.au3>Local $my_prog = "c:\somewhere\bin\my_prog.exe"Local $pid = Run(@ComSpec & " /k " & $my_prog)Sleep(100)Send ("C")Sleep(50)Send ("MS")Sleep(100)Send ("0{ENTER}1{ENTER}1{ENTER}2{Enter}")But I when I tried to hook up the STDIO anduse the StdoutRead()/StdinWrite() so that Ican check the output strings, outputs tothe screen didn't show up anywhere. I'm very new to Autoit, I basically used yourcode as samples to my code. The following codefailed:Do you see any fault in the code above?/Why TeaWhyTea, I understand that you want to get your problem solved, but so do a lot of other users. It is considered rude to post in someone elses thread asking for help (especially in Example Scripts, where support do not belong at all). Another thing that is considered rude is double posting: http://www.autoitscript.com/forum/index.php?showtopic=101683Please keep this in mind for next time. You are forgiven already. github.com/jvanegmond Link to comment Share on other sites More sharing options...
whim Posted September 10, 2009 Share Posted September 10, 2009 --snip--Local $pid = Run(@ComSpec, "c:\somewhere\bin\", @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)--snip--/Why Teamissing space before c:\somewhere ...wim Link to comment Share on other sites More sharing options...
WhyTea Posted September 10, 2009 Share Posted September 10, 2009 WhyTea, I understand that you want to get your problem solved, but so do a lot of other users. It is considered rude to post in someone elses thread asking for help (especially in Example Scripts, where support do not belong at all). Another thing that is considered rude is double posting: http://www.autoitscript.com/forum/index.php?showtopic=101683Please keep this in mind for next time. You are forgiven already.Thanks for the gentle nudge. It won't happen again. It's just a desperate new user try to get an answer to a seemingly basic problem Link to comment Share on other sites More sharing options...
WhyTea Posted September 10, 2009 Share Posted September 10, 2009 As far as I see the problem is with triggering the output to the console... in my example above the line StdinWrite($foo, "exit" & @CRLF); send exit command to @ComSpec to trigger stdout! does this. But I guess you also need a command for your app to close first (to make ComSpec accept "exit")? Try something like StdinWrite($pid, "Q" & @CRLF) ; send some quit command to your app to exit back to console! or... even processclose($pid) might work ...but I'm not sure about that. #include <Constants.au3> Local $pid = Run(@ComSpec, "c:\", @SW_SHOW, $STDIN_CHILD + $STDERR_MERGED) StdinWrite ($pid, "my_prog.exe" & @CRLF) Sleep(150) StdinWrite($pid, "C") ; no CR needed Sleep(50) StdinWrite($pid, "M") ; no CR needed Sleep(50) StdinWrite($pid, "S") ; no CR needed Sleep(50) StdinWrite($pid, "0" & @CRLF) StdinWrite($pid, "1" & @CRLF) StdinWrite($pid, "1" & @CRLF) StdinWrite($pid, "2" & @CRLF) StdinWrite($pid, "Q" & @CRLF) ; send some quit command to your app to exit back to console! StdinWrite($foo, "exit" & @CRLF); send exit command to @ComSpec to trigger stdout! While 1 $data &= StdoutRead($pid) If @error Then ExitLoop Wend ConsoleWrite($data) If StringInStr($data, "<009>") <> 0 Then MsgBox(0, "Test Passed!", $data) Else MsgBox(0, "Test Failed!", $data) EndIf Thanks for the reply. I did all those before my last post. Nothing worked. I will get out of here as I shouldn't have budge in in the first place. Sorry about that! Link to comment Share on other sites More sharing options...
sinistlor Posted January 3, 2010 Share Posted January 3, 2010 Oh wow, this is EXACTLY what I was looking for! This may be the answer to using gpg in batch mode with autoit. I will report my findings. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now