file-seq

added
1.0

ns
clojure.core

type
function

(file-seq dir)

A tree seq on java.io.Files

                ;; first create a Java File object using the file function in
;; the clojure.java.io package and use that object to create a file-seq
;; then show the first and first 10 members of that seq

(def f (clojure.java.io/file "c:\\\\clojure-1.2.0"))
;;=> #'user/f
(def fs (file-seq f))
;;=> #'user/fs
(first fs)
;;=> #<File c:\\clojure-1.2.0>
(clojure.pprint/pprint (take 10 fs))
;; (#<File c:\\clojure-1.2.0>
;;  #<File c:\\clojure-1.2.0\\.gitignore>
;;  #<File c:\\clojure-1.2.0\\build.xml>
;;  #<File c:\\clojure-1.2.0\\changes.txt>
;;  #<File c:\\clojure-1.2.0\\cl.bat>
;;  #<File c:\\clojure-1.2.0\\clojure.jar>
;;  #<File c:\\clojure-1.2.0\\doc>
;;  #<File c:\\clojure-1.2.0\\doc\\clojure>
;;  #<File c:\\clojure-1.2.0\\doc\\clojure\\pprint>
;;  #<File c:\\clojure-1.2.0\\doc\\clojure\\pprint\\CommonLispFormat.markdown>)
;;=>
            
                ;; Fill an array with the file names in a directory.
;; Put them in an array.
(mapv str (filter #(.isFile %) (file-seq (clojure.java.io/file "."))))

;; Use the Path utilities to do glob filtering.
(let [grammar-matcher (.getPathMatcher 
                          (java.nio.file.FileSystems/getDefault)
                          "glob:*.{g4,md}")]
  (->> "."
       clojure.java.io/file
       file-seq
       (filter #(.isFile %))
       (filter #(.matches grammar-matcher (.getFileName (.toPath %))))
       (mapv #(.getAbsolutePath %))))

;; At that point it may be better to just use the java.nio.file classes.
;; https://github.com/ajoberstar/ike.cljj has some ideas