files from tree

Started by HPW, October 21, 2005, 01:48:28 AM

Previous topic - Next topic

HPW


(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)))))



(filestree "c:/temp" ".*.*" 0 '())


Any improvments?
Hans-Peter

HPW

#1
Now with optional path:



(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)))))



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

Dmi

#2
Just check it.

In fact, there is a _much_ more faster way, based on the code from "tips and tricks":
(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)))))

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

HPW

#3
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:



(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:



(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

#4
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