44

I have a populated fileset and I need to print the matching filenames into a text file.

I tried this:

<fileset id="myfileset" dir="../sounds">
    <include name="*.wav" />
    <include name="*.ogg" />
</fileset>

<property name="sounds" refid="myfileset" />
<echo file="sounds.txt">${sounds}</echo>

which prints all the files on a single line, separated by semicolons. I need to have one file per line. How can I do this without resorting to calling OS commands or writing Java code?

UPDATE:

Ah, should have been more specific - the list must not contain directories. I'm marking ChssPly76's as the accepted answer anyway, since the pathconvert command was exactly what I was missing. To strip the directories and list only the filenames, I used the "flatten" mapper.

Here is the script that I ended up with:

<fileset id="sounds_fileset" dir="../sound">
    <include name="*.wav" />
    <include name="*.ogg" />
</fileset>

<pathconvert pathsep="&#xA;" property="sounds" refid="sounds_fileset">
    <mapper type="flatten" />
</pathconvert>

<echo file="sounds.txt">${sounds}</echo>
martin clayton
  • 76,436
  • 32
  • 213
  • 198
Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92

2 Answers2

69

Use the PathConvert task:

<fileset id="myfileset" dir="../sounds">
    <include name="*.wav" />
    <include name="*.ogg" />
</fileset>

<pathconvert pathsep="${line.separator}" property="sounds" refid="myfileset">
    <!-- Add this if you want the path stripped -->
    <mapper>
        <flattenmapper />
    </mapper>
</pathconvert>
<echo file="sounds.txt">${sounds}</echo>
Michael-O
  • 18,123
  • 6
  • 55
  • 121
ChssPly76
  • 99,456
  • 24
  • 206
  • 195
  • The Pathconvert task seems to create the absolute file path. Is there a possibility to prevent this? I have the same issue and want to print a list of files but not the full path but a relative path (so flatten won't work for me) – Soccertrash Jul 31 '12 at 10:41
  • 1
    @Soccertrash, I solved this with post-processing the file with `` to remove the common base dir prefix (in my case). – mgaert Jan 24 '13 at 12:04
  • 3
    @mgaert, you can do it in-place as ``. Place it within ``. – Alexander Solovets Nov 26 '13 at 12:42
4

Since Ant 1.6 you can use toString:

<echo file="sounds.txt">${toString:myfileset}</echo>
R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77