Created
November 19, 2017 19:06
-
-
Save mslinn/077e951c36acc1afa7ff23cc8a680fc1 to your computer and use it in GitHub Desktop.
Deletes files and directories recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object Nuke { | |
| import java.io.IOException | |
| import java.nio.file.{Files, Paths, Path, SimpleFileVisitor, FileVisitResult} | |
| import java.nio.file.attribute.BasicFileAttributes | |
| import sbt.Logger | |
| /** Adapted from https://stackoverflow.com/a/45703150/553865 */ | |
| def remove(root: Path, deleteRoot: Boolean = true) | |
| (implicit log: Logger): Unit = { | |
| log.debug( | |
| if (deleteRoot) s"Nuking $root" | |
| else s"Clearing files and directories under $root (${ root.toFile.listFiles.mkString(", ") })" | |
| ) | |
| Files.walkFileTree(root, new SimpleFileVisitor[Path] { | |
| override def visitFile(file: Path, attributes: BasicFileAttributes): FileVisitResult = { | |
| Files.delete(file) | |
| FileVisitResult.CONTINUE | |
| } | |
| override def postVisitDirectory(dir: Path, exception: IOException): FileVisitResult = { | |
| if (deleteRoot) Files.delete(dir) | |
| FileVisitResult.CONTINUE | |
| } | |
| }) | |
| } | |
| def removeUnder(string: String) | |
| (implicit log: Logger): Unit = remove(Paths.get(string), deleteRoot=false) | |
| def removeAll(string: String) | |
| (implicit log: Logger): Unit = remove(Paths.get(string)) | |
| def removeUnder(file: java.io.File) | |
| (implicit log: Logger): Unit = remove(file.toPath, deleteRoot=false) | |
| def removeAll(file: java.io.File) | |
| (implicit log: Logger): Unit = remove(file.toPath) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment