- Without a console window popping up
- Without redirecting command output to a file using > or >> and then reading the file
Code: Select all
runWaitMany(commands) {
; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99
shell := ComObjCreate("WScript.Shell")
; Open cmd.exe with echoing of commands disabled
exec := shell.Exec(ComSpec " /Q /K echo off")
; Send the commands to execute, separated by newline
exec.StdIn.WriteLine(commands "`n" "exit") ; Always exit at the end!
; Read and return the output of all commands
Return exec.StdOut.ReadAll()
}
Code: Select all
MsgBox,,,% RunWaitMany("
(
echo Put your commands here,
echo each one will be run,
echo and you'll get the output.
)")
Code: Select all
; Open a console window for this demonstration:
DllCall("AllocConsole")
; Open the application's stdin/stdout streams in newline-translated mode.
stdin := FileOpen("*", "r `n") ; Requires v1.1.17+
stdout := FileOpen("*", "w `n")
stdout.Write("Enter your query.`n\> ")
stdout.Read(0) ; Flush the write buffer.
query := RTrim(stdin.ReadLine(), "`n")
stdout.WriteLine("Your query was '" query "'. Have a nice day.")
stdout.Read(0) ; Flush the write buffer.
Sleep 5000
https://stackoverflow.com/questions/322 ... using-exec
Where I've needed this to date, I have the console command output redirected to a temporary file, then read the temp file contents and delete.
- That is not always sufficient if a console command hangs (I don't get anything back because it never finishes executing)
- Writing a temporary file and reading it back... surely there must be a better way. And there is. But re-coding some of that stuff on Stack Overflow looks like it would take some time to get it working.