Search code examples
lispfilesystemsdirectorycommon-lisp

How do I iterate through a directory in Common Lisp?


I'm using OpenMCL on Darwin, and I'd like to do something like:

(loop for f in (directory "somedir")
  collect (some-per-file-processing f))

But I can't get directory to return anything other than NIL, and I can't seem to find any good explanation online (other than "its different for each system").

Any pointers?


Solution

  • Does your pathname specification contain a wildcard? Common Lisp's pathname stuff is somewhat hard to grasp at first - at least for me it was... As the CLHS states on the directory function:

    If the pathspec is not wild, the resulting list will contain either zero or one elements.

    In order to have your pathname include a wildcard, you might try the make-pathname function, like

    (directory (make-pathname :directory '(:absolute "srv" "hunchentoot") :name :wild :type "lisp"))
    

    Or even

    (directory (make-pathname :directory '(:absolute "srv" "hunchentoot") :name :wild :type :wild))
    

    I found the CL-FAD library a great help for dealing with pathnames and the file system. In particular, its list-directory function might be easier to use than the plain standard directory function.