tags:

views:

4782

answers:

3

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
+2  A: 

try:

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


ThankYou flicken !

Blauohr
To delete directories, you'll need to add ncludeemptydirs="true", as outlined below.
flicken
+7  A: 

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.

jamesh
+1  A: 

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.

skanjo