tags:

views:

726

answers:

2

I'm trying to create a comma-delimited list of files or directories under the current directory. For instance, suppose I have the following folder structure:

Root
-- Directory1
-- Directory2
...

I want to generate a variable or property that contain "Directory1,Directory2." I've tried iterating (using ant-contrib "for" task) over a <dirset dir="." includes="*">, but this generates absolute paths; I've then extracted the file names using the "basename" task, however that in turn generates an output property. Since properties are immutable, what I get in practice is "Directory1,Directory1,..."

Is there a saner way of doing this, or will I have to write a Java extension to do this for me?

+2  A: 

The pathconvert task can be used to format a dirset with arbitrary separators:

<dirset id="dirs" dir="." includes="*"/>
<pathconvert dirsep="," pathsep="/" property="dirs" refid="dirs"/>
<echo message="${dirs}"/>
Jörn Horstmann
Took a bit more effort (using globmapper and some other minor hacks) and I'm pretty sure you switched dirsep with pathsep in your example, but you pointed me in the exact direction I needed. Thanks!
Tomer Gabel
A: 

Just confirming Jörn's answer was exactly what I needed (as a starting point) as well.

<dirset id="dirset.sandbox" dir="${sandbox.dir}" includes="*">
  <exclude name="output"/>
</dirset>
<pathconvert pathsep=" " property="dirs.sandbox" refid="dirset.sandbox">
  <mapper type="flatten"/>
</pathconvert>
<echo message="[*** the sandbox dir list is ${dirs.sandbox} ***]"/>

sandbox.dir is an absolute path similar to /root/build/workspace and contains several subdirectories. The output is a space-separated list of those directories.

AndrewRich