Page 1 of 1
files from tree
Posted: Fri Oct 21, 2005 8:48 am
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?
Posted: Fri Oct 21, 2005 9:19 am
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 '())
Posted: Fri Oct 21, 2005 8:59 pm
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"))
Posted: Sat Oct 22, 2005 10:45 am
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.
Posted: Sat Oct 22, 2005 12:49 pm
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