2012-08-14 159 views
0

我目前正在使用AutoIt编写命令行应用程序,并且无法使其打印回到打开它的同一命令行。我的目标是让整个程序在一个shell中工作。以下是我试过最初:如何在不使用AutoIt命令行打开新shell的情况下运行命令

;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) 

然后我编译它,并通过命令行运行它(每个代码框代表一个新的shell):

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

打开新的外壳 ↓

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

一旦完成,我的目录中列出的文件退出,并且

打开新的外壳 ↓

The command completed successfully. 

而且该窗口马上关闭。

我要找的输出是同样的事情,但在一个窗口中,像这样:

回答

1

您编译项目作为该控制台应用程序来运行。您可以通过在编译时检查有点隐名的“创建CUI而不是GUI EXE”复选框来完成此操作。然后从shell中调用myprogram.exe而不是.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 
+0

标签,这在显示输出方面奇妙地工作,但不幸的是仍然不能在同一个shell中运行命令,只是显示输出。这很重要,因为一些命令,例如启动管理提示的“runas/noprofile/user:Administrator cmd.exe”,需要一个提示密码,该密码不能通过命令本身传递。为此,我已将我的项目迁移到C++ – 2012-08-20 22:22:25

+2

Matthew,对于runas命令,在AutoIt中有一个名为RunAs的特殊函数,您可以在其中传递用户名和密码。 – 2014-07-17 09:03:38

0

因为这个问题没有答案:

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

这样既命令在同一个CMD-Box的执行。