P.S. Edi, this code also includes a portable directory walker that
applies a function to all the pathnames under a given root, something
I think you were asking about a few weeks ago.
(defun list-directory (dirname)
"Return a list of the contents of the directory named by dirname.
Names of subdirectories will be returned in `directory normal form'.
Unlike CL:DIRECTORY, LIST-DIRECTORY does not accept wildcard
pathnames; `dirname' should simply be a pathname that names a
directory. It can be in either file or directory form."
(let ((wildcard (make-pathname :name :wild
:defaults (pathname-as-directory dirname))))
#+(or sbcl cmu lispworks)
;; SBCL, CMUCL, and Lispworks return subdirectories in directory
;; form just the way we want.
;; OpenMCl by default doesn't return subdirectories at all. But
;; when prodded to do so with the special argument :directories,
;; it returns them in directory form.
(directory wildcard :directories t)
;; Allegro normally return directories in file form but we can
;; change that with the :directories-are-files argument.
(directory wildcard :directories-are-files nil)
;; CLISP has a particularly idiosyncratic (though arguably
;; logically consistent) view of things. And (as of 2.32) it has a
;; slight bug to work around. But we can bludgeon even it into
;; doing what we want.
;; CLISP 2.32 won't list files without an extension when :type is
;; wild so we make a special wildcard for it.
(directory (make-pathname :type nil :defaults wildcard))
;; And CLISP doesn't consider subdirectories to match unless
;; there is a :wild in the directory component.
:directory (append (pathname-directory wildcard) (list :wild))
(defun pathname-as-directory (pathname)
"Return a pathname reperesenting the given pathname in `directory
form', i.e. with all the name elements in the directory component and
NIL in the name and type components. Can not be used on wild
pathnames. Returns its argument if name and type are both nil or
(setf pathname (pathname pathname))
(when (wild-pathname-p pathname)
(error "Can't reliably convert wild pathnames to directory names."))
((or (component-present-p (pathname-name pathname))
(component-present-p (pathname-type pathname)))
:directory (append (pathname-directory pathname) (list (file-namestring pathname)))
(defun walk-directory (dirname fn &key (depth-first t) (directories t))
"Walk a directory invoking `fn' on each pathname found. If
`depth-first' is t (the default) walks depth-first. If `directories'
is t (the default) invokes `fn' on directory pathnames as well."
(do ((files (list (pathname-as-directory dirname))))
(let ((f (pop files)))
(when directories (funcall fn f))
(let ((children (list-directory f)))
(nconc children files)
(nconc files children)))))
(t (funcall fn f))))))
(defun directory-pathname-p (p)
"Is the given pathname the name of a directory? This function can
usefully be used to test whether a name returned by LIST-DIRECTORIES
or passed to the function in WALK-DIRECTORY is the name of a directory
in the file system since they always return names in `directory normal
(not (component-present-p (pathname-name p)))
(not (component-present-p (pathname-type p)))))
(defun component-present-p (value)
(and value (not (eql value :unspecific))))
Peter Seibel pe...@javamonkey.com
Lisp is the red pill. -- John Fraser, comp.lang.lisp
I like this! I've wanted this sort of functionality for a project of
license is it under, if I may ask? I would want your permission
it, of course.
I hereby place it in the public domain. Go nuts. Better yet, convince
your favorite vendor (assuming it's not SBCL, CMUCL, or Lispworks who
already do) to make their native CL:DIRECTORY function to return
subdirectories when given a wild pathname like:
(make-pathname :name :wild :type :wild :directory whatever)
and to always return the names of directories in "directory form",
thus the LIST-DIRECTORIES function that much less necessary. (If
anyone cares, I have a theory about how such an implementation of
DIRECTORY can also be made compatible with PATHNAME-MATCH-P.)
Soon I should have a FILE-EXISTS-P function that papers over the wide
range of behaviors exhibited by different implementations when given
the name of a directory and which can also be used to tell whether a
file is a directory or a file.