From: Paul Reiners
Subject: Iterating through files in subdirectories in a directory
Date: 
Message-ID: <c13dd925-eaca-4bc2-a004-a27dddd92f0f@d45g2000hsc.googlegroups.com>
Given a directory, I'm trying to iterate through all the files in all
the subdirectories of that directory.  Right now I have the following
code:

  (let* ((master-dir-name "C:/MyFolder/*")
         (directories (directory master-dir-name)))
    (dolist (dir directories)
      (let* ((dir-name (directory-namestring dir))
             (backslash-pos (position #\\ dir-name :from-end t :start
0
                                      :end (- (length dir-name) 2)))
             (short-dir-name (subseq dir-name (1+ backslash-pos) (-
(length dir-name) 1))))
        (format t "~A~%" short-dir-name)
        (let ((files (directory (format nil "C:~A*" dir-name))))
          (dolist (file files)
            (format t "~T~A~%" file))))))

This does give me all the subdirectories of C:/MyFolder/, but it
doesn't list the files in any of the subdirectories.  What am I doing
wrong here?  Is there a good reference available somewhere on stepping
through directories?

From: Paul Reiners
Subject: Re: Iterating through files in subdirectories in a directory
Date: 
Message-ID: <5b68f8fd-99b0-4566-ab6c-43a75c981b7f@t54g2000hsg.googlegroups.com>
On Apr 3, 11:59 am, Paul Reiners <············@gmail.com> wrote:
> Given a directory, I'm trying to iterate through all the files in all
> the subdirectories of that directory.
>
> Is there a good reference available somewhere on stepping
> through directories?

I found a way of doing this:

  (let* ((master-dir-name "C:/MyFolder/*")
         (directories (directory master-dir-name)))
    (dolist (dir directories)
      (let* ((dir-name (directory-namestring dir))
             (backslash-pos (position #\\ dir-name :from-end t :start
0
                                      :end (- (length dir-name) 2)))
             (short-dir-name (subseq dir-name (1+ backslash-pos) (-
(length dir-name) 1))))
        (format t "~A~%" short-dir-name)
        (let ((files (directory (format nil "C:~A*.*" (substitute #\\ #
\/ dir-name)))))
          (dolist (file files)
            (format t "~T~A~%" file)))))))

But I don't like this, since it's not OS-independent.  If someone can
point me to a good reference on working with paths, files, and
directories in Lisp, I'd appreciate it.
From: Richard M Kreuter
Subject: Re: Iterating through files in subdirectories in a directory
Date: 
Message-ID: <877ifex2d7.fsf@progn.net>
Paul Reiners <············@gmail.com> writes:

> Given a directory, I'm trying to iterate through all the files in all
> the subdirectories of that directory.

This stuff is fairly implementation-dependent, and implementations'
treatment of files, namestrings, wild pathnames, and truenames differ,
and so in general DIRECTORY does something different everywhere.  What
implementation are you using?

> Right now I have the following code:
>
>   (let* ((master-dir-name "C:/MyFolder/*")
>          (directories (directory master-dir-name)))
>     (dolist (dir directories)

Why do you think that all the elements of DIRECTORIES name
directories?  Under most implementations, the call to DIRECTORY will
return a pathname for each file and directory under C:\\MyFolder\\.

>       (let* ((dir-name (directory-namestring dir))
>              (backslash-pos (position #\\ dir-name :from-end t :start
> 0
>                                       :end (- (length dir-name) 2)))
>              (short-dir-name (subseq dir-name (1+ backslash-pos) (-
> (length dir-name) 1))))

You probably don't need to go through this work to get the last
directory; (last (pathname-directory dir)) should do.


>         (format t "~A~%" short-dir-name)
>         (let ((files (directory (format nil "C:~A*" dir-name))))

I can only guess why this call to DIRECTORY doesn't work, but it might
help things slightly to avoid using namestrings, e.g.,

          (let ((files (directory (merge-pathnames
                                   (make-pathname :name :wild :type :wild)
                                   dir))))
              ...)

>           (dolist (file files)
>             (format t "~T~A~%" file))))))
>
> This does give me all the subdirectories of C:/MyFolder/, but it
> doesn't list the files in any of the subdirectories.

--
RmK
From: John Thingstad
Subject: Re: Iterating through files in subdirectories in a directory
Date: 
Message-ID: <op.t81p18qbut4oq5@pandora.alfanett.no>
P� Thu, 03 Apr 2008 18:59:18 +0200, skrev Paul Reiners  
<············@gmail.com>:

> Given a directory, I'm trying to iterate through all the files in all
> the subdirectories of that directory.  Right now I have the following
> code:
>
>   (let* ((master-dir-name "C:/MyFolder/*")
>          (directories (directory master-dir-name)))
>     (dolist (dir directories)
>       (let* ((dir-name (directory-namestring dir))
>              (backslash-pos (position #\\ dir-name :from-end t :start
> 0
>                                       :end (- (length dir-name) 2)))
>              (short-dir-name (subseq dir-name (1+ backslash-pos) (-
> (length dir-name) 1))))
>         (format t "~A~%" short-dir-name)
>         (let ((files (directory (format nil "C:~A*" dir-name))))
>           (dolist (file files)
>             (format t "~T~A~%" file))))))
>
> This does give me all the subdirectories of C:/MyFolder/, but it
> doesn't list the files in any of the subdirectories.  What am I doing
> wrong here?  Is there a good reference available somewhere on stepping
> through directories?

CL-FAD http://www.weitz.de/cl-fad/

use walk-directory

--------------
John Thingstad
From: Paul Reiners
Subject: Re: Iterating through files in subdirectories in a directory
Date: 
Message-ID: <0eef818a-4173-49ad-a1f9-210a58b2ea92@c65g2000hsa.googlegroups.com>
On Apr 3, 12:40 pm, "John Thingstad" <·······@online.no> wrote:
> På Thu, 03 Apr 2008 18:59:18 +0200, skrev Paul Reiners
> <············@gmail.com>:
>
> CL-FADhttp://www.weitz.de/cl-fad/


Thanks.  Exactly what I'm looking for.  In fact, I ordered a copy of
_Practical Common Lisp_ by Peter Seibel, who wrote that library.
From: Pascal J. Bourguignon
Subject: Re: Iterating through files in subdirectories in a directory
Date: 
Message-ID: <7cabkaowim.fsf@pbourguignon.anevia.com>
Paul Reiners <············@gmail.com> writes:

> Given a directory, I'm trying to iterate through all the files in all
> the subdirectories of that directory.  Right now I have the following
> code:

   (directory "/some/dir/**/*.*")
or
   (directory "/some/dir/**/*")

might be all you need in your implementation/OS.

But it may be more complex:

(remove-if-not ; the directory below should be enough, but unfortunately
               ; some implementation consider directories as files, just
               ; because the underlying file system do.
   (lambda (item)
      (ignore-errors ; some implementation signal an error when the 
                     ; argument to probe-file is not a file!
         (probe-file item)))
   (directory (merge-pathnames
                 (make-pathname :directory '(:relative :wild-inferiors) 
                                :name :wild :type :wild) 
                 dir-path
                 nil))) ; portable merge-pathnames...

Unfortunately, DIRECTORY may break with persmission denied on some
OS/implementation, when there is one unaccessible item in the tree. So
the above is not totally portable either.  

You'll have great fun if you want to deal portably with CL pathnames
and files...

-- 
__Pascal Bourguignon__