lisp-tips / lisp-tips

Common Lisp tips. Share !
119 stars 2 forks source link

Traversing (walking) directories #21

Open vindarel opened 5 years ago

vindarel commented 5 years ago

See uiop:collect-sub*directories in uiop/filesystem. It takes as arguments:

Given a directory, when collectp returns true with the directory, call the collector function on the directory, and recurse each of its subdirectories on which recursep returns true.

This function will thus let you traverse a filesystem hierarchy, superseding the functionality of cl-fad:walk-directory.

The behavior in presence of symlinks is not portable. Use IOlib to handle such situations.

Example:

(defparameter *dirs* nil "All recursive directories.")

(uiop:collect-sub*directories "~/cl-cookbook"
    (constantly t)
    (constantly t)
    (lambda (it) (push it *dirs*)))
svetlyak40wt commented 5 years ago

Also, cl-fad:walk-directory can be used if you want to collect not only subdirectories, but also the files:

(cl-fad:walk-directory "./"
     (lambda (name)
        (format t "~A~%" name))
svetlyak40wt commented 5 years ago

By the way, in your example, you can use (constantly t) instead of (lambda (it) t).

vindarel commented 5 years ago

perfect, thanks.