Recursion and getting folder's complete contents

Ben Rubinstein benr_mc at cogapp.com
Tue May 7 12:30:01 EDT 2002


Also note that while this is a classic case for a recursive function, if
you're not comfortable with recursion (or concerned about the lack of tail
recursion optimisation, etc...) it's not too yukky to do this in a
non-recursive way.

Adapting David's code:

 function walkDir dirPath
   put empty into tList
   put return into folders2do
   repeat until folders2do = empty
     put line 1 of folders2do into subPath
     delete line 1 of folders2do
     set defaultFolder to dirPath & subPath
     put the long files into fList
     repeat for each line fLine in fList
       if char 1 of fLine <> "." then
          put item 1 of fLine & comma & last item of fLine into fData
          put dirPath & subPath & "/" & fData & return after tList
        end if
     end repeat
     get the folders
     repeat for each line x in it
       if char 1 of x <> "." then
         put ("/" & subPath) & return after folders2do
       end if
     end repeat
   end repeat
   return tList
 end walkDir

PS David noted that the function would exclude invisible files - I think
this is probably true only on Unix systems?  If you want to use this
function on MacOS preX or Windows systems, you might want to comment out the
tests for filename not starting with ".", to avoid confusion.
 
  Ben Rubinstein               |  Email: benr_mc at cogapp.com
  Cognitive Applications Ltd   |  Phone: +44 (0)1273-821600
  http://www.cogapp.com        |  Fax  : +44 (0)1273-728866





More information about the use-livecode mailing list