How can I print a fileset to a file, one file name

2019-01-31 00:14发布

问题:

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>

回答1:

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>


回答2:

Since Ant 1.6 you can use toString:

<echo file="sounds.txt">${toString:myfileset}</echo>


标签: ant fileset