How can I print a fileset to a file, one file name per line?
Asked Answered
H

2

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>
Hughhughes answered 21/9, 2009 at 21:5 Comment(0)
R
70

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>
Rik answered 21/9, 2009 at 21:11 Comment(3)
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)Weissmann
@Soccertrash, I solved this with post-processing the file with <replace file="${sounds.txt}" token="${basedir}\" value="" /> to remove the common base dir prefix (in my case).Albric
@mgaert, you can do it in-place as <map from="${basedir} to=""/>. Place it within <pathconvert>.Bayly
R
4

Since Ant 1.6 you can use toString:

<echo file="sounds.txt">${toString:myfileset}</echo>
Reliance answered 2/2, 2017 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.