Instantly share code, notes, and snippets.
Fantastic. This is very useful on Windows when you can't delete insane directory hierarchies because of the MAX_PATH limitation. Just fire a "lein repl", load your script and (delete-recursively "insane/path").
Thanks a lot
this does the same using the build-in file-seq function to enumerate the contents of the directory.
(defn delete-recursively [fname]
(doseq [f (reverse (file-seq (clojure.java.io/file fname)))]
It's elegant, but I'm not sure it's as robust for large trees as the recursive version. reverse removes the laziness of file-seq.
Using letfn will simplify things a bit (no need for (func func )). Also you can expose the silently argument:
(defn delete-files-recursively [fname & [silently]]
(letfn [(delete-f [file]
(when (.isDirectory file)
(doseq [child-file (.listFiles file)]
(clojure.java.io/delete-file file silently))]
(delete-f (clojure.java.io/file fname))))