0

I am currently writing a command line application in AutoIt and am having trouble with making it print back to the same command line I opened it in. My goal is to have the entire program work within a single shell. Here is what I tried initially:

;myprogram.au3
$MyCommand = 'dir'
Run(@ComSpec & " /c " & $MyCommand, @SystemDir, @SW_Show)
Run(@ComSpec & " /c @echo off && echo Command completed successfully. && @echo on", @SystemDir, @SW_Show)

Then I compiled it and ran it via the command line (each code box represents a new shell):

C:\Users\Matthew>myprogram.au3
C:\Users\Matthew>

Opens new shell ↓

Volume in drive C has no label.
Volume Serial Number is 0287-990C
Directory of C:\Users\Matthew
<Finishes normal dir command output>

Once finished, listing files in my directory that exits and

Opens new shell ↓

The command completed successfully.

And that window closes immediately.

The output I am looking for is the same thing, but in one window, like this:

C:\Users\Matthew>myprogram
*Output of dir command*
The command completed successfully
C:\Users\Matthew>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Dorian
  • 1,884
  • 1
  • 19
  • 29

2 Answers2

1

You compile your project as a console application for this to work. You can do that by checking the somewhat cryptically named "Create CUI instead of GUI EXE" checkbox when compiling. Then call myprogram.exe from the shell instead of .au3.

#include <Constants.au3>

;myprogram.au3
$MyCommand = 'dir'
Local $foo = Run(@ComSpec & " /c " & $MyCommand & " \& echo Command completed successfully.", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

Local $output
While 1
    $output = StdoutRead($foo)
    If @error Then ExitLoop
    ConsoleWrite($output)
Wend
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andy Raddatz
  • 2,792
  • 1
  • 27
  • 29
  • Tag, this works marvelously in terms of displaying output, but unfortunately still can't actually run the command in the same shell, just display the output. This matters because some commands, suchh as "runas /noprofile /user:Administrator cmd.exe", which starts an admin prompt, require a prompted password, that can't be passed via the command itself. To this end i have migrated my projectto C++ – John Dorian Aug 20 '12 at 22:22
  • 2
    Matthew, for the runas command there is a special function called RunAs in AutoIt in which you can pass the username and password. – Jos van Egmond Jul 17 '14 at 09:03
0

Since this question is unanswered:

$MyCommand = 'ping www.google.com && ping www.stackoverflow.com'
Run(@ComSpec & " /c " & $MyCommand, @SystemDir, @SW_Show)

Like this both commands are executed in the same CMD-Box.

SophieXLove64
  • 316
  • 1
  • 11