2010-05-10 91 views
2

我想用螞蟻與以下記錄從CMD樹命令的輸出:如何使用Apache Ant exec任務記錄cmd tree命令的輸出?

<exec dir="${basedir}" executable="cmd" output="output.txt"> 
     <arg value="tree" /> 
    </exec> 

但是,我看到在「output.txt的」以下內容:

Microsoft Windows XP [Version 5.1.2600] 
    (C) Copyright 1985-2001 Microsoft Corp. 

當我運行CMD在Windows命令:

C:\tree>tree 

我得到的是這樣的:

C:\tree 
     └───test 
      └───test 

誰能告訴我如何編寫一個Ant腳本來將樹結構打印到文件中?

回答

4

您試圖執行tree.com。從documentation of exec

[...]特別是,如果你不把一個 文件擴展名的可執行文件,只有 「.EXE」文件都找了,沒有 「.COM」,「 .CMD「或其他文件類型 中列出的環境變量 PATHEXT。這隻被 shell使用。

您需要明確地致電tree.com

<exec dir="${basedir}" executable="tree.com" output="output.txt" /> 

另一種方式是指定的cmd/C參數,這是對我工作:

<exec dir="${basedir}" executable="cmd" output="output.txt"> 
    <arg value="/C" /> 
    <arg value="tree" /> 
</exec> 
+0

非常感謝,這也適用於我。 – 2010-05-10 15:45:30

+0

@ S.N:請檢查我的更新答案。 – 2010-05-10 15:55:09

+0

我想過提示'cmd/c',但認爲調用一個shell來調用一個控制檯程序有點多餘。不知道那個螞蟻忽略了PATHEXT :-) – Joey 2010-05-10 16:58:08

1

(猜測這裏,我不是螞蟻用戶)

,如果您鍵入

cmd tree 

進入命令提示符,你也不會看到超過

Microsoft Windows [Version 6.1.7600] 
Copyright (c) 2009 Microsoft Corporation. All rights reserved. 

剛剛執行tree呢?

<exec dir="${basedir}" executable="tree" output="output.txt"/> 
+0

+1'tree'是一個普通的命令行應用程序('tree.com' ),而不是一個內置的命令,所以沒有必要調用'cmd' – 2010-05-10 14:48:29

+0

那麼,我得到以下錯誤與建議的方法: 「無法運行程序」樹「:CreateProcess錯誤= 2,系統找不到指定的文件「。 – 2010-05-10 15:08:57

+0

您可能需要稍微更改一下。 TREE [drive:] [path] [/ F] [/ A] 因此可執行文件=「TREE $ {basedir}」 – NitroxDM 2010-05-10 15:19:35