files from tree

For the Compleat Fan
Locked
HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

files from tree

Post by HPW »

Code: Select all

(define (filestree fpath fpattern intflag retlst)
 (dolist (nde (directory fpath)) 
   (if (and (directory? (append fpath "/" nde)) (!= nde ".") (!= nde "..")) 
     (setq retlst (append (filestree (append fpath "/" nde) fpattern intflag retlst )))
     (if (and (!= nde ".") (!= nde ".."))
       (if (regex fpattern nde intflag)
         (setq retlst (append retlst (list nde)))
         (setq retlst retlst))
       (setq retlst retlst)))))

Code: Select all

(filestree "c:/temp" ".*\.*" 0 '())
Any improvments?
Hans-Peter

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Now with optional path:

Code: Select all

(define (filestree fpath fpattern intflag pathbool retlst)
 (dolist (nde (directory fpath)) 
   (if (and (directory? (append fpath "/" nde)) (!= nde ".") (!= nde "..")) 
     (setq retlst (append (filestree (append fpath "/" nde) fpattern intflag pathbool retlst )))
     (if (and (!= nde ".") (!= nde ".."))
       (if (regex fpattern nde intflag)
         (if pathbool
           (setq retlst (append retlst (list (string fpath "/" nde))))
           (setq retlst (append retlst (list nde))))
         (setq retlst retlst))
       (setq retlst retlst)))))

Code: Select all

; (filestree "c:/temp" ".*\.*" 0 nil '())
Hans-Peter

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Just check it.
In fact, there is a _much_ more faster way, based on the code from "tips and tricks":

Code: Select all

(define (show-tree dir)
  (let (files '())
   (dolist (nde (directory dir))
    (if (and (directory? (append dir "/" nde)) (!= nde ".") (!= nde ".."))
     (set 'files (append files (show-tree (append dir "/" nde))))
     (push (append dir "/" nde) files)
     (reverse files)))))

Code: Select all

(filter
  (lambda (x) (regex ".*newlisp.*\.*" x))
  (show-tree "/usr/local"))
WBR, Dmi

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Dmi, thanks for your hints!
(I still have problems to think "lispy" the right way, old horse and new tricks etc.)
Taken your code and optimized for more speed:

Code: Select all

(define (show-tree dir)
  (let (files '())
   (dolist (nde (directory dir))
    (if (and (directory? (append dir "/" nde)) (!= nde ".") (!= nde ".."))
     (set 'files (append files (show-tree (append dir "/" nde))))
     (push (append dir "/" nde) files -1)))files))
PS: reverse seems not to be cheap! ;-)

Taken my code with your ideas is similar fast but more flexibel:

Code: Select all

(define (filestree fpath fpattern intflag pathbool)
 (let (retlst '())
 (dolist (nde (directory fpath))
   (if (and (directory? (append fpath "/" nde)) (!= nde ".") (!= nde ".."))
     (setq retlst (append retlst (filestree (append fpath "/" nde) fpattern intflag pathbool)))
     (if (and (!= nde ".") (!= nde ".."))
       (if (regex fpattern nde intflag)
         (if pathbool
           (push (append fpath "/" nde) retlst -1)
           (push nde retlst -1))))))retlst))
My list does not contain "." and ".."
The regex only matches on the filename (not the path)
Path-return is optional.
The regex flag is passed through.
Hans-Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

The example with 'reverse' is old. At that time pushing to the end of a list using (push item aList -1) was not optimized. But now this is clearly the better, faster solution.

Lutz

Locked