From: Vijay L
Subject: recursing directories
Date: 
Message-ID: <1eaf81aa.0310222250.1b3d0665@posting.google.com>
I'd like to know how to write a program that forms a tree of the
directory structure given some directory.  I couldn't find an
appropriate function in CLtL2.

I found DIRECTORY but it only gives the files in the directory [if
given as (directory "d:/*") that is] but not the subdirectories.  I'd
like to be able to recurse through the whole directory structure.

Any help appreciated.

I'm using clisp 2.31 on Windows NT

Cheers,
Vijay

All future commitments are optimistic.

From: Nikodemus Siivola
Subject: Re: recursing directories
Date: 
Message-ID: <bn829i$jci$1@nyytiset.pp.htv.fi>
Vijay L <······@lycos.com> wrote:

> I'd like to know how to write a program that forms a tree of the
> directory structure given some directory.  

1. 

 As incredible as it may sound, this cannot be done both
 reliably and portably.

2.

 (set-union (directory (make-pathname :name :wild 
                                      :type :wild 
                                      :default dir))
            (directory (make-pathname :name nil 
                                      :type nil
                                      :directory (append
                                                  (pathname-directory dir)
                                                  :wild))))

 Should get you most of the way there. No guarantees. You should look
 for the newsgroup articles on this by Kent Pitman for more details and
 howto.
            
> I'm using clisp 2.31 on Windows NT

3. <<plug>>

 If you were lucky enough to be on a platform supported by
 asdf-install and UFFI, you could:

* (require :asdf-install)
                         
("SB-EXECUTABLE" "SB-GROVEL" "SB-POSIX" "SB-BSD-SOCKETS" "ASDF-INSTALL")
* (asdf-install:install :osicat)
;; snippage plethora
* (osicat:mapdir (lambda (e) (cons e (osicat:file-kind e))) "/tmp/")
                                                                    

((#P"/tmp/." . :DIRECTORY) (#P"/tmp/.." . :DIRECTORY)
 (#P"/tmp/.X11-unix" . :DIRECTORY) (#P"/tmp/.font-unix" . :DIRECTORY)
 (#P"/tmp/.gdm_socket" . :SOCKET) (#P"/tmp/.X64-lock" . :REGULAR-FILE)
 (#P"/tmp/.ICE-unix" . :DIRECTORY) (#P"/tmp/.X0-lock" . :REGULAR-FILE)
 (#P"/tmp/ssh-FelyK478" . :DIRECTORY) (#P"/tmp/esrv1000-rhino" . :SOCKET))
* (osicat:with-directory-iterator (next "/tmp/")
     (loop for entry = (next)
           while entry
           when (member :other-read (osicat:file-permissions entry))
           collect entry))
                          
(#P"/tmp/." #P"/tmp/.." #P"/tmp/.X11-unix" #P"/tmp/.font-unix"
 #P"/tmp/.gdm_socket" #P"/tmp/.X64-lock" #P"/tmp/.ICE-unix" #P"/tmp/.X0-lock")
* 

Cheers,

 -- Nikodemus
From: Madhu
Subject: Re: recursing directories
Date: 
Message-ID: <xfed6cot31s.fsf@shelby.cs.unm.edu>
* Nikodemus Siivola  <············@nyytiset.pp.htv.fi> :

| Vijay L <······@lycos.com> wrote:
|
|> I'd like to know how to write a program that forms a tree of the
|> directory structure given some directory.  
|
| 1. 
|
|  As incredible as it may sound, this cannot be done both
|  reliably and portably.
|
| 2.
|
|  (set-union (directory (make-pathname :name :wild 
|                                       :type :wild 
|                                       :default dir))
|             (directory (make-pathname :name nil 
|                                       :type nil
|                                       :directory (append
|                                                   (pathname-directory dir)
|                                                   :wild))))
|
|  Should get you most of the way there. No guarantees. You should look
|  for the newsgroup articles on this by Kent Pitman for more details and
|  howto.
|             
|> I'm using clisp 2.31 on Windows NT
|

CLISP provides CLISP specific extension EXT:PROBE-DIRECTORY which you
can probably use like this if you wanted to get a TREE of all file
pathnames rooted at directory p:

* (defun %TREE (p &optional (depth 0))
   (if (ignore-errors (ext:probe-directory p))
      (cons (directory (make-pathname :name :wild :type :wild
                                      :defaults p))
            (loop for c in (directory (make-pathname :defaults p
                                  :directory (append (pathname-directory p) 
                                                     (list :wild))))
                  collect (%TREE c (1+ depth))))))


Recursing through directory structures is fairly flexible in lisp. For
example If I wanted to build tag files for all .lisp files in all
subdirectories, I can do:

(labels ((recursion-considered-harmful (p &optional (depth 0))
   (if (ext:probe-directory p) 
     (nconc (directory (make-pathname :name :wild :type "lisp"
                                      :defaults p))
         (loop for c in (directory (make-pathname :defaults p
                     :directory (append (pathname-directory p) (list
                     :wild))))
               nconc (recursion-considered-harmful c (1+   depth)))))))
    (apply #'ltags (recursion-considered-harmful  #p"cl-ppcre-0.5.7/")))

where "recursion-considered-harmful" is just a name for an anonymous
recursive function I'm forced to name. I don't really think recursion
is harmful :>

Madhu
(from around Chepauk where the rains that put the match out have now
stoppped :)
From: Vijay L
Subject: Re: recursing directories
Date: 
Message-ID: <1eaf81aa.0310262123.6389183a@posting.google.com>
Madhu <·····@cs.unm.edu> wrote in message 
> 
> CLISP provides CLISP specific extension EXT:PROBE-DIRECTORY which you
> can probably use like this if you wanted to get a TREE of all file
> pathnames rooted at directory p:
> 
> * (defun %TREE (p &optional (depth 0))
>    (if (ignore-errors (ext:probe-directory p))
>       (cons (directory (make-pathname :name :wild :type :wild
>                                       :defaults p))
>             (loop for c in (directory (make-pathname :defaults p
>                                   :directory (append (pathname-directory p) 
>                                                      (list :wild))))
>                   collect (%TREE c (1+ depth))))))

Thanks.  It works like a charm.
 
> Recursing through directory structures is fairly flexible in lisp. For
> example If I wanted to build tag files for all .lisp files in all
> subdirectories, I can do:
> 
> (labels ((recursion-considered-harmful (p &optional (depth 0))
>    (if (ext:probe-directory p) 
>      (nconc (directory (make-pathname :name :wild :type "lisp"
>                                       :defaults p))
>          (loop for c in (directory (make-pathname :defaults p
>                      :directory (append (pathname-directory p) (list
>                      :wild))))
>                nconc (recursion-considered-harmful c (1+   depth)))))))
>     (apply #'ltags (recursion-considered-harmful  #p"cl-ppcre-0.5.7/")))


> where "recursion-considered-harmful" is just a name for an anonymous
> recursive function I'm forced to name. I don't really think recursion
> is harmful :>
I don't think any lisper does :)
 
> Madhu
> (from around Chepauk where the rains that put the match out have now
> stoppped :)

Cheers,
Vijay
(A pity indeed.  Anyway, we won yesterday and hopefully I'll have fun
at the Chinnaswamy Stadium :)
From: Pascal Bourguignon
Subject: Re: recursing directories
Date: 
Message-ID: <878ynccjsl.fsf@thalassa.informatimago.com>
Nikodemus Siivola <······@random-state.net> writes:

> Vijay L <······@lycos.com> wrote:
> 
> > I'd like to know how to write a program that forms a tree of the
> > directory structure given some directory.  
> 
> 1. 
> 
>  As incredible as it may sound, this cannot be done both
>  reliably and portably.

Yep.  It's not  a wonder  the first  module (and  so far  the  only) I
implemented in my SUSV3 package is the DIRENT one.


> > I'm using clisp 2.31 on Windows NT

Well, while it's  a POSIX interface, I did it  with the LINUX package,
so I guess Vijay you'll have to implement your own on MS-Windows-NT...


-- 
__Pascal_Bourguignon__
http://www.informatimago.com/