19
votes

I need an Apache Ant target that deletes all files in a directory but does not touch subdirectories.

In my current approach I have to explicitly name the subdirectories I want to skip (atm just "src/").

<delete>
   <fileset dir="${dist.dir}" excludes="src/" />
</delete>

But I don't like it. That way I would have to modify the target everytime something changes in the subdirectory structure.

Any ideas?

1

1 Answers

45
votes

This should work:

<delete>
   <fileset dir="${dist.dir}">
      <include name="*"/>
   </fileset>
</delete>

The * wildcard should only delete the files at the top level, not directories or subdirectories. If you wanted it to be recursive, you'd need to use **/* instead.