Mr Pablo
Mr Pablo

Reputation: 4188

Ant task to find all sub-directories that contain a certain file

I'm struggling to figure out this task in Ant. There are not many examples in the documentation and I can;t seem to find a solid answer online.

I need to scan a directory that contains several sub-directories eg

I need to run the build.xml from Root, and with a task scan all sub-directories of TargetFolder (so Folder A, B, C etc) and if the directory contains a file, foo.txt, run an <apply> task for that directory.

I'm able to do a <dirset> and get the list of sub-directories. And I can do a separate <fileset> to scan TargetFolder and get all occurences of foo.txt. But, I have no clue how to combine these things, or how to go about doing a simple file check attached to the <apply> task.

Upvotes: 1

Views: 236

Answers (1)

martin clayton
martin clayton

Reputation: 78125

Here is one way. Use a dirset as you say, with the <present> selector:

<dirset id="dirs" dir="Root">
  <present targetdir="Root">
    <mapper type="glob" from="*" to="*/foo.txt" />
  </present>
</dirset>
    
<apply executable="ls">
  <arg value="-alF" />
  <dirset refid="dirs" />
</apply>

You could merge both into one task:

<apply executable="ls">
  <arg value="-alF" />
  <dirset id="dirs" dir="Root">
    <present targetdir="Root">
      <mapper type="glob" from="*" to="*/foo.txt" />
    </present>
  </dirset>
</apply>

Upvotes: 1

Related Questions