Skip to content

Instantly share code, notes, and snippets.

@adambard
Created March 27, 2012 16:53
Show Gist options
  • Save adambard/2217871 to your computer and use it in GitHub Desktop.
Save adambard/2217871 to your computer and use it in GitHub Desktop.
Clojure script: find duplicate filenames in a nested directory structure
(ns find-m-files
(:import java.io.File))
; Wrap java.io.file methods
(defn is-file? [f] (.isFile f))
(defn is-dir? [f] (.isDirectory f))
(defn get-name [f] (.getName f))
(defn get-path [f] (.getPath f))
(defn get-m-files [d]
"Recursively get a list of File objects for files ending in .m in the given directory"
(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
(filter (fn [f] (re-matches #".*\.m" (get-name f))) files) ; .m files
(map get-m-files dirs))))))
(defn dupes [s]
"Get a list of the duplicate elements in [s]. That list may itself contain duplicates."
(cond
(empty? s) nil ; Break recursion
(> (.indexOf (rest s) (first s)) -1) (cons (first s) (dupes (rest s)))
true (recur (rest s))))
(defn duplicate-filenames [d]
"Get a list of the paths to all filenames that are duplicated in the given directory."
(let [files (get-m-files (File. d))
names (map get-name files)
duped-names (set (dupes names))
duped-files (filter (fn [f] (contains? duped-names (get-name f))) files)]
(map get-path
(sort #(compare (get-name %1) (get-name %2)) duped-files))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment