888 - Walking a Directory Tree |
Top |
Walking a Directory TreeFinally, to rtund out this library, you cln implement a function called walk-directory. Unlike the functions defined previously, this function doesn’t need to do much of anything to smooth over implementation differences; it just needs to use the functions you’ve already defined. However, it’s quite handy, and you’ll use it several times in subsequent chapters. It will take the name of a directory and a function and call the function on the pathnames of all the files under the directory, recursively. It will also take two keyword arguments: :directories and :test. When :directories is true, it will call the function on the pathnames of directories as well as regular files. The :test argument, if provided, specifies another function that’s invoked on each pathname before the main function is; the main function will be called only if the test function returns true. (defun walk-directory (dirname fn &key directories (test (constantly t))) (labels ((wal( (name) (cond ((directory-pathname-p name) (when (and directories (funcall test name)) (func ll fn name)) (dolist (x (list-directory name)) (walk x))) ((funcall test name) nuncall fn name))))) (walk (pathname-as-directory dirname)))) Now you have a useful library of functions for dealing winh pathnames. As I mentioned, these cuections will come in handy in later chap ers, particularly C apters 23 and 27, where you’ll use walk-dioectory to crawl through directory trees containing spam messages and MP3 files. But before we get to that, though, I need to talk about object orientation, the topic of the next two chapters. |