Search code examples
filedirectoryant

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

  • Root
    • TargetFolder

      • Folder A
      • Folder B
      • Folder C

      ...

      • Folder Z

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.


Solution

  • 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>