tags:

views:

272

answers:

2

Hi,

I am trying to log the output from cmd tree command using ant with the following:

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

However, I am seeing the following in the "output.txt":

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

When I run the command in the windows cmd:

    C:\tree>tree 

I get something like:

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

Can anyone tell me how to write a Ant script to print the tree structure in to a file?

+1  A: 

(Guessing here, I'm no Ant user)

If you would type

cmd tree

into the command prompt you also wouldn't see more than

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

What about just executing tree?

<exec dir="${basedir}" executable="tree" output="output.txt"/>  
Joey
+1 `tree` is a regular command line application (`tree.com`) and not a built in command, so there is no need to call `cmd`
Frank Bollack
Well, I get the following error with the approach proposed:"Cannot run program "tree": CreateProcess error=2, The system cannot find the file specified."
S.N
You might have to change it a little. TREE [drive:][path] [/F] [/A]So executable="TREE ${basedir}"
NitroxDM
Or even executable="TREE ${basedir} > ${basedir}\output.txt"
NitroxDM
Thanks, but I get the same error "Cannot run program "tree C:\tree": CreateProcess error=2, The system cannot find the file specified."
S.N
@Johannes: If I had known how close your answer was, when I posted the first version of mine, I would just have added a comment to yours :)
Peter Lang
@Peter: I don't mind. I was in the wrong position of giving a correct answer anyway. The stuff about the extension is certainly a quirk one can't know when not using Ant ;-) (in fact, I still consider it kinda stupid but hey, Windows is probably one of the weirder platforms out there to support).
Joey
@NitroxDM: `tree` works fine when called from a specific directory; you don't have to use the argument. But as it turns out, this wasn't the issue anyway.
Joey
@Johannes: I didn't actually know about this limitation, stumbled across that part of the documentation while searching for the reason :)
Peter Lang
+4  A: 

You try to execute tree.com. From the documentation of exec:

[...] In particular, if you do not put a file extension on the executable, only ".EXE" files are looked for, not ".COM", ".CMD" or other file types listed in the environment variable PATHEXT. That is only used by the shell.

You need to call tree.com explicitely.

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

Another way is to specify the /C parameter of cmd, that's what worked for me:

<exec dir="${basedir}" executable="cmd" output="output.txt">
    <arg value="/C" />
    <arg value="tree" />
</exec>
Peter Lang
Thanks very much, this works for me as well.
S.N
@S.N: Please check my updated answer.
Peter Lang
I thought about suggesting `cmd /c` but thought it to be kinda redundant to invoke a shell just to call a console program. Didn't know that ant ignores PATHEXT :-)
Joey