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?
To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r .
This task is used to delete a single file, directory or subdirectories. We can also delete set of files by specifying set of files. It does not remove empty directory by default, we need to use includeEmptyDirs attribute to remove that directory.
You can use the FileUtils. cleanDirectory() method to recursively delete all files and subdirectories within a directory, without deleting the directory itself. To delete a directory recursively and everything in it, you can use the FileUtils.
To delete the specified directory and all its subdirectories, use the Directory. Delete() method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With