Re: does anybody have a recursive directory subrountine???
Re: does anybody have a recursive directory subrountine???
- Subject: Re: does anybody have a recursive directory subrountine???
- From: Ric Phillips <email@hidden>
- Date: Thu, 13 Sep 2001 11:27:17 +1000
On 13/9/01 10:53 AM, "Andrew Simpson" <email@hidden>
wrote:
>
I would like a subrountine that creates a list of all available files in all
>
avaialable folders no matter how deep they go into the hierachi from a
>
choosen disk or volume
>
>
anybody have any code, just lying around to do this???
>
>
much appreciated
>
>
Andrew.
I've used the following heaps of times to process folders recursively. You
can put your own do-stuff-with-contents-of-current-folder code where the
comments indicate. Note: E-Mail clients can wrap code snippets and screw up
the display - in the code below '\n' stands for 'newline' where ever it is
there is supposed to be a line break after it, if there is no '\n' at the
end of a line then the break is not supposed to be there. '\n' is not part
of the script code - remove it before trying to use the script.
-- Start code snippet
on processTree(root)\n
tell application "Finder"\n
-- Code to prcess files at current level\n
if (count of every file in folder root) > 0 then\n
set filesInRoot to the name of every file in folder root as list\n
repeat with thisfile in filesInRoot\n
try\n
-- Place specific file processing code here.\n
-- eg: get the name or path and add it to a global list.\n
-- Note: Try wrapper. Lack of error handling on one \n
-- exception will halt all processing on tree.\n
-- Put in your own error routine (to write an error log\n
-- or just display an informative dialog for example)\n
end try\n
end repeat\n
end if\n
-- Code for Re-entrant navigation of tree\n
if (count of every folder in folder root) > 0 then\n
set theseFolders to the name of every folkder in folder root as list\n
repeat with thisFolder in theseFolders\n
set oldRoot to root\n
set root to root & thisFolder & ":"\n
my processTree(root)\n
set root to oldRoot\n
end repeat\n
end if\n
end tell\n
end processTree \n
-- End code snippet.
There will be, of course, alternative ways to code this - some more elegant
than mine.
If you have REALLY DEEP nests of folders, you might like to capture stack
overflow errors - put a try around the 'my processTree(root)' line - but I
don't really know what the stack limits are on AppleScript, or whether they
expand dynamically with the memory allocation. (Though I would up the memory
on any recursive script substantially above the 200K default, just to be
cautious!)
Remember that any variable (ie. a list of file names) defined inside this
handler is local to the handler, and each time it calls itself a new version
of that variable will be created (and destroyed - hopefully - when the
currently running instance returns.) If you wish to preserve data in a
variable from call to call you must either (1) pass the values into an
instance via the call parameters and then back out via the return command,
or (2) use a global variable (object declarations are best) and use it from
inside each instance. The second method is going to be simpler and more
efficient for the kind of data you wish to collect.
If you want a 'pre-packaged' approach, Jon's commands scripting additions
has a 'walk folders' command that will achieve the same thing. (Though -
IMHO everyone should learn the basics of re-entrant (recursive) code, just
for their own edification).
Happy trails,
Ric Phillips
Computer Laboratory Support Officer
Faculty of Humanities and Social Sciences
La Trobe University
9479 2792