Created
January 18, 2012 21:52
-
-
Save tce/1636017 to your computer and use it in GitHub Desktop.
clojure directory listings
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
(comment "wildcardfilter from http://corfield.org/blog/post.cfm/real-world-clojure-powermta-log-files") | |
(defn- wildcard-filter | |
"Given a regex, return a FilenameFilter that matches." | |
[re] | |
(reify java.io.FilenameFilter | |
(accept [_ dir name] (not (nil? (re-find re name)))))) | |
(defn- nonhidden-filter | |
"return a FilenameFilter that ignores files that begin with dot or end with ~." | |
[] | |
(reify java.io.FilenameFilter | |
(accept [_ dir name] (and (not (.startsWith name ".")) | |
(not (.endsWith name "~")))))) | |
(defn directory-list | |
"Given a directory and a regex, return a sorted seq of matching filenames. To find something like *.txt you would pass in \".*\\\\.txt\"" | |
([dir re] | |
(sort (.list (clojure.java.io/file dir) (wildcard-filter (java.util.regex.Pattern/compile re))))) | |
([dir] | |
(sort (.list (clojure.java.io/file dir) (nonhidden-filter)))) | |
) | |
(defn full-directory-list | |
"Given a directory, return the full pathnames for the files it contains" | |
[dir] | |
(sort (map #(.getCanonicalPath %) (.listFiles (clojure.java.io/file dir)))) | |
) | |
(defn pwd [] | |
(.getCanonicalPath (clojure.java.io/file "."))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment