Skip to content

Instantly share code, notes, and snippets.

@duanebester
Created October 15, 2020 19:30
Show Gist options
  • Save duanebester/a3ea84e412e770decd5259a95533426f to your computer and use it in GitHub Desktop.
Save duanebester/a3ea84e412e770decd5259a95533426f to your computer and use it in GitHub Desktop.
Get all nested File objects from a directory | Clojure
;; Lovingly stolen from: https://gist.github.com/adambard/2217871
(require '[clojure.java.io :as io])
(defn is-file? [f] (.isFile f))
(defn is-dir? [f] (.isDirectory f))
(defn get-name [f] (.getName f))
(defn get-path [f] (.getPath f))
(def dir-path "/Users/<username>/Downloads/data")
(def dir (io/file dir-path))
(defn get-nested-files [d]
(let [ls (if (is-dir? d) (.listFiles d) nil)
files (filter is-file? ls)
dirs (filter is-dir? ls)]
(if (nil? ls)
nil
(flatten (concat
files
(map get-nested-files dirs))))))
(get-nested-files dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment