Delete all files in a directory w/o subdirectories with Apache Ant

tyrondis picture tyrondis · Oct 19, 2010 · Viewed 23.7k times · Source

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?

Answer

skaffman picture skaffman · Oct 19, 2010

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.