I can't figure out how to delete all contents of a directory.
For cleaning out a directory, I want to remove all files and directories inside it: I want to wipe everything there is inside (files and directories).
I tried this with the delete task, but I can't figure out to make it also remove directories and not just files. I've tried different ways to specify the directories, but nothing works.
task deleteGraphicsAssets(type:Delete) { delete fileTree('src').include('**/*') }
.
task deleteGraphicsAssets(type:Delete) { delete fileTree('src').include('/') }
.
task deleteGraphicsAssets(type:Delete) { delete fileTree('src').include('*') }
Any help appreciated!
Edit:
This works - yet it seems a bit like a hack.
task deleteGraphicsAssets(type: Delete) { def dirName = "src" delete dirName doLast { file(dirName).mkdirs() } }
I was looking for something like:
task deleteGraphicsAssets(type: Delete) { deleteContentsOfDirectory "src" }
or
task deleteGraphicsAssets(type: Delete) { delete {dir : "src", keepRoot : true } }
To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.
rmdir is a command-line utility for deleting empty directories, while with rm you can remove directories and their contents recursively.
Using Java I/O Package listFiles() method to list all files and sub-directories in the directory. For each file, we recursively call deleteDir() method. In the end, we delete the directory using File. delete() .
clean will delete a build directory, all your classes will be removed for a fresh compile. assemble will create an archive (JAR) file from your Java project and will run all other tasks that require compile, test and so on.
To delete the src
directory and all its contents:
task deleteGraphicsAssets(type: Delete) { delete "src" }
Following will delete all content from the src folder but leaves the folder itself untouched:
task deleteGraphicsAssets(type: Delete) { def dirName = "src" file( dirName ).list().each{ f -> delete "${dirName}/${f}" } }
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