lisp-tips icon indicating copy to clipboard operation
lisp-tips copied to clipboard

Traversing (walking) directories

Open vindarel opened this issue 6 years ago • 3 comments

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

  • a directory
  • a recursep function
  • a collectp function
  • a collector function

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*)))

vindarel avatar Sep 18 '19 11:09 vindarel

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 avatar Sep 18 '19 13:09 svetlyak40wt

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

svetlyak40wt avatar Sep 18 '19 13:09 svetlyak40wt

perfect, thanks.

vindarel avatar Sep 18 '19 13:09 vindarel