Page 1 of 1

RunWait Output of command

Posted: 12 Mar 2014, 09:26
by Proxx
Hi all,

Autohotkey lacks the support to run a command en retrieve the output.
something like:

Code: Select all

RunWait, %ComSpec% /c ping 127.0.0.1, <PIDvar>, <outputVAR>
and the outputVAR would hold the ping results as result of running the ping command from my example
now i generally do something like

Code: Select all

RunWait, %ComSpec% /c ping 127.0.0.1 > results.log
Fileread, resultsvar, results.log
Filedelete, results.log
or use a lib to do this but the ones i used never were perfect.

is this something we can expect in the future? i'm really missing this in almost all my AHK projects.

Re: RunWait Output of command

Posted: 18 Mar 2014, 04:10
by Proxx
Nobody ?

Am i really the only one that is experiencing this problem?
or am i missing something.

Re: RunWait Output of command

Posted: 21 Mar 2014, 17:53
by Zelio
If you mean something like http://www.autohotkey.com/board/topic/9 ... douttovar/ then I hope that it will be built-in.

Re: RunWait Output of command

Posted: 24 Mar 2014, 04:37
by Proxx
yes stdout is exactly what i mean :)

but i prefer the native support instead of using functions.

Re: RunWait Output of command

Posted: 30 Aug 2014, 05:56
by Dougal
I wanted same thing (specifically for exit code for multiple robocopy processes) and achieved it via com object using wscript (which is built into most versions of windows).
Simple example of use:

Code: Select all

oWSH := ComObjCreate("WScript.Shell")
oApp := oWSH.Exec("Calc.exe")
while (oApp.Status = 0) {
	Sleep, 500
} 
MsgBox, % oPP.ExitCode
ExitApp
Apart from Status and ExitCode properties, there are also StdOut, StdIn and StdErr.
More information at http://msdn.microsoft.com/en-us/library ... 84%29.aspx.

For multiple processes I added each WshScriptExec object to an array, then used timer to wait for them to exit and get the exit code.

This has added advantage that I can process the output before appending to log file so I can have the progress indicators in the command output, but removed from the log - otherwise you have to have one or the other in both.

You can also access StdOut while process is running. StdOut is a text stream and has an Eof property to tell you when you have reached end of available output. You can also just wait till process has finished (Status = 1), and read the whole stream.

Cheers

Re: RunWait Output of command

Posted: 30 Aug 2014, 20:14
by lexikos
Example (for v2):

Code: Select all

shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(A_ComSpec " /Q /k echo off")
commands := "
(
echo doing some stuff.
echo doing some more stuff...
echo all done!
exit
)"
exec.StdIn.WriteLine(commands)
MsgBox % exec.StdOut.ReadAll()
I posted a v1 version of this and another example here.

I'll add something like this to the help file.
RunWait, %ComSpec% /c ping 127.0.0.1, <PIDvar>, <outputVAR>
Firstly, you seem to have omitted the WorkingDir and Max|Min|Hide|UseErrorLevel parameters which come before OutputVarPID. Secondly, OutputVarPID itself is pretty useless for RunWait - you have to use it from another thread, since by the time RunWait returns, the PID is invalid. That being the case, it would make sense to replace OutputVarPID with OutputVarOutput.

If someone really needs the PID, they can use Run and then ProcessWaitClose.

Also, since this is the v2 forum, you presumably mean %A_ComSpec% rather than %ComSpec%. ;)

Re: RunWait Output of command

Posted: 30 Aug 2014, 23:18
by Dougal
Just found some weirdness while playing with .exec. When using exec("ipconfig.exe /all"), status doesn't change and console doesn't close untill all StdOut has been read. So using a loop to wait for Status to become 1 before using StdOut.RaedAll() never ends. Using StdOut.ReadAll() inside the loop to concatenate to variable and then displaying the variable when the status becomes 1 and loop exits does work.

Other commands like tracert.exe do close the console as soon as command has finished, and StdOut can still be read to get output.

The main utility of wscript.shell for me is ability to launch apps without waiting, then monitor until they end and get both exit code and output when they do. I couldn't find any other easy way to achieve this in AHK - RunWait can get exitcode but not continue processing or get output, Run can get output with some effort, but not exit code (unless I am mistaken?), Wscript.Shell .exec does both easily.