Last active
October 1, 2020 12:31
-
-
Save borkdude/2a85971164a7e71282d0d0a59fb4b60f to your computer and use it in GitHub Desktop.
Delete file tree recursively using Java NIO
This file contains 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
#!/usr/bin/env bb | |
;; requires bb 0.2.2 or higher | |
(require '[clojure.java.io :as io]) | |
(defn delete-recursively [f] | |
(let [dir (io/file f)] | |
(if (.exists dir) | |
(do (java.nio.file.Files/walkFileTree | |
(.toPath dir) | |
(reify java.nio.file.FileVisitor | |
(preVisitDirectory [this p attrs] | |
java.nio.file.FileVisitResult/CONTINUE) | |
(postVisitDirectory [this p attrs] | |
(.delete (io/file (.toFile ^java.nio.file.Path p))) | |
java.nio.file.FileVisitResult/CONTINUE) | |
(visitFile [this p attrs] | |
(.delete (io/file (.toFile ^java.nio.file.Path p))) | |
java.nio.file.FileVisitResult/CONTINUE))) | |
true) | |
false))) | |
(delete-recursively (first *command-line-args*)) | |
;; $ mkdir -p /tmp/foo/bar/baz | |
;; $ delete.clj /tmp/foo | |
;; true | |
;; $ delete.clj /tmp/foo | |
;; false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment