How do I delete a dirset of directories with Ant?

27,352

Solution 1

Here's the answer that worked for me:

<delete includeemptydirs="true">
    <fileset dir="${root}" defaultexcludes="false">
       <include name="**/*tmp*/**" />
    </fileset>
</delete>

I had an added complication I needed to remove .svn directories too. With defaultexcludes, .* files were being excluded, and so the empty directories weren't really empty, and so weren't getting removed.

The attribute includeemptydirs (thanks, flicken, XL-Plüschhase) enables the trailing ** wildcard to match the an empty string.

Solution 2

try:

<delete includeemptydirs="true">
    <fileset dir="${root}">
          <include name="**/*tmp*/*" />
    </fileset>
</delete>

ThankYou flicken !

Solution 3

I just wanted to add that the part of the solution that worked for me was appending /** to the end of the include path. I tried the following to delete Eclipse .settings directories:

<delete includeemptydirs="true">
    <fileset dir="${basedir}" includes"**/.settings">
</delete>

but it did not work until I changed it to the following:

<delete includeemptydirs="true">
    <fileset dir="${basedir}" includes"**/.settings/**">
</delete>

For some reason appending /** to the path deletes files in the matching directory, all files in all sub-directories, the sub-directories, and the matching directories. Appending /* only deletes files in the matching directory but will not delete the matching directory.

Share:
27,352
Ishyc
Author by

Ishyc

This space is intentionally blank.

Updated on February 26, 2020

Comments

  • Ishyc
    Ishyc about 4 years

    I want to delete all directories and subdirectories under a root directory that are contain "tmp" in their names. This should include any .svn files too. My first guess is to use

    <delete>
        <dirset dir="${root}">
              <include name="**/*tmp*" />
        </dirset>
    </delete>
    

    This does not seem to work as you can't nest a dirset in a delete tag.

    Is this a correct approach, or should I be doing something else?

    • ant version == 1.6.5.
    • java version == 1.6.0_04