Search code examples
fileantpathfind

Ant: Find the path of a file in a directory


I want to find the path of a file in a directory (similar to unix 'find' command or the 'which' command, but I need it to work platform-independent) and save it as a property.

Tried to use the whichresource ant task, but it doesn't do the trick (I think it's only good for looking inside jar files).

I would prefer if it would be pure ant and not to write my own task or use a 3rd-party extension.

Notice that there might be several instances of a file by that name in the path - I want it to only return the first instance (or at least I want to be able to choose only one).

Any suggestions?


Solution

  • One possibility is to use the first resource selector. For example to find a file called a.jar somewhere under directory jars:

    <first id="first">
        <fileset dir="jars" includes="**/a.jar" />
    </first>
    <echo message="${toString:first}" />
    

    If there are no matching files nothing will be echoed, otherwise you'll get the path to the first match.