[REBOL] Re: Scan for files through deep directory
From: SunandaDH::aol::com at: 5-Mar-2006 20:26
Thorsten:
> Perhaps you have some advice for me.
I compared the runstats with my standard folder search routine. Yours seems
to need twice the working space. But that's still nowhere near close to 500Meg.
If the comment in your code is for real ("useful to avoid stack overflow")
then you must have subfolders more than 8000 deep...When you said deeply nested,
you weren't joking!
The precise max stack depth depends on the version of REBOL and the platform
you run on. But if you have had to tweak code to avoid the max stack depth,
maybe it is time to refactor as a non-recursive routine.
***
As an example, here is two versions of my own folder search. The first is
recursive, and the second depth-first iterative. They don't do *quite* the same
as yours (they return a block of all files that matched at least one of the
target substrings). But you could easily adapt them -- or post-process the
output
Sunanda.
===================
find-files-recursive: func [
folder [file!]
targets [block!]
/local
matches
][
matches: copy []
foreach file read folder [
either dir? join folder file [
append matches find-files-recursive join folder file targets
][
file: to-string file
foreach target targets [
if find/any file target [
append matches join folder file
break
] ;; if
] ;; for
] ;; either
] ;; for
return matches
]
print length? res: find-files-recursive %/c/ [".txt" "*log"]
===================
find-files-iterative: func [
folder [file!]
targets [block!]
/local
matches
folders-to-search
current-folder
][
matches: copy []
folders-to-search: copy []
insert folders-to-search folder
while [0 <> length? folders-to-search][
current-folder: folders-to-search/1
remove folders-to-search ;; drop first entry
foreach file read current-folder [
either dir? join current-folder file [
insert folders-to-search join current-folder file
][
file: to-string file
foreach target targets [
if find/any file target [
append matches join folder file
break
] ;; if
] ;; for
] ;; either
] ;; for
] ;;while
return matches
]
print length? res: find-files-iterative %/c/ [".txt" "*log"]