The Directory Walker revisited - closure?

David Vaughan dvk at dvkconsult.com.au
Sat Sep 6 01:03:00 EDT 2003


On Saturday, Sep 6, 2003, at 12:49 Australia/Brisbane, Geoff Canyon 
<gcanyon at inspiredlogic.com> wrote:
>
> On Friday, September 5, 2003, at 05:47  PM, David Vaughan wrote:
>
>> Not in the terminology but in reading of recursive code. The process 
>> above is indeed depth-first and will recurse only to the maxDepth. 
>> The next folder at the same level will not be processed until all 
>> lower folders have been processed, and that statement is of course 
>> (recursively) true for each lower folder. Thus, depth matters, 
>> breadth is irrelevant. You can test this by walking a small tree 
>> while displaying depth and breadth in real time.
>>
>> If the processing were post-order rather than pre-order then breadth 
>> would count but in Geoff's and my code it does not. I think this is 
>> where the principal confusion has arisen.
>
> In fact, I did write exactly such a counter, just to be certain I 
> wasn't mistaken.
>
> regards,
>
> Geoff Canyon
>

Below is my code (essentially the same as Geoff's independent work) 
fixed in accord with Dar's discovery that it was permissions (leading 
to endless recycling) which were blowing up the code, not depth (nor, 
of course, breadth).

Within it are some depth-checking lines commented out. I used it 
(returning counts rather than files) to count the major directories on 
my OS X system of 20GB content, with the following results:
Folder			maxDepth	files
Applications			16		100535
System (OS X)			17		59356
Library				13		38937
Users				10		15475
System Folder (OS 9)	5		1373

regards
David

-- This recursive function expects a folder path.
-- It returns a file list for that folder and for each
--  sub-folder it contains (pre-order search)
-- Invisible files are excluded.
function walkDir dirPath
   -- add 1 to isDepth
   -- if isDepth > limitDepth then
   -- put isDepth into limitDepth
   -- put limitDepth
   -- end if
   put empty into tList
   set defaultFolder to dirPath
   -- Dar's discovery. Check permissions were ok
   get the Result
   if it is not empty then
     -- subtract 1 from isDepth
     return empty
   end if
   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 & "/" & 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 walkDir(dirPath & "/" & x) after tList
     end if
   end repeat
   -- subtract 1 from isDepth
   return tList
end walkDir




More information about the use-livecode mailing list