Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get the filenames of all files in a directory with ANT

Tags:

ant

For example

/test/a.jar
/test/b.jar
/test/c.jar

output:
a.jar
b.jar
c.jar

This works for the most part, except it only seems to be getting one of the files, not all =/

       <for param="file">
            <path>
                <fileset dir="${test.dir}/lib">
                    <include name="**/*.jar"/>
                </fileset>
            </path>
            <sequential>
                <basename property="filename" file="@{file}"/>
                <echo message="${filename}"/>
            </sequential>
        </for>

This just gets me:

c.jar
c.jar
c.jar
like image 501
Th3sandm4n Avatar asked Jan 19 '23 01:01

Th3sandm4n


2 Answers

The problem you've hit is the immutability of Ant properties - once a property is set the value cannot normally be changed.

The first time around the loop the filename property is being set, and that value 'sticks'.

Since Ant 1.8 the local task allows you to localise a property to the current execution block. For example, your sequential would be:

<sequential>
    <local name="filename" />
    <basename property="filename" file="@{file}"/>
    <echo message="${filename}"/>
</sequential>

Ant forgets the property at the end of the sequential, so a new value can be used in each iteration.

like image 138
martin clayton Avatar answered Mar 22 '23 22:03

martin clayton


It's been a while since I've done anything with ant, but have you tried a foreach?

   <foreach>
        <fileset dir="${test.dir}/lib">
            <include name="**/*.jar"/>
        </fileset>
        <echo message="${foreach.file}"/>
   </foreach>
like image 35
Jeff Lauder Avatar answered Mar 22 '23 22:03

Jeff Lauder