Searching Directories for a File

Given a tree-structured directory of files and a file path, we want to find the file specified by the path. We will use a Lisp representation in which a symbol is a file or directory name and a list is a directory whose first element is the directory name and whose rest is the contents.


(defun findpath (dirtree path)
  (if (null dirtree)
      nil                          ; file not found
      (if (consp (first dirtree))  ; directory?
          (if (eq (first path)       ; dir name ==
                  (first (first dirtree)))
              (findpath              ; yes: go in
                (rest (first dirtree)) ; dir contents
                (rest path))           ; pop path
              (findpath              ; no: keep
                (rest dirtree)       ;     looking
                path))
          (if (eq (first path)       ; file name ==
                  (first dirtree))
              (first path)           ; yes: success
              (findpath              ; no: keep
                (rest dirtree)       ;     looking
                path)) ) ) )

Contents    Page-10    Prev    Next    Page+10    Index