Re: Finder - Delete empty folders, recursively
Re: Finder - Delete empty folders, recursively
- Subject: Re: Finder - Delete empty folders, recursively
- From: Graff <email@hidden>
- Date: Mon, 14 Jun 2004 20:56:23 -0400
Whups, I just realized I ripped out the line that deletes the folder
and left in the testing dialog. Here's the real version:
----
tell application "Finder"
set theFolder to (folder of the front window)
my ClearEmptyFolder(theFolder)
end tell
on ClearEmptyFolder(whatsPassed)
tell application "Finder"
set theFolders to folders of whatsPassed
repeat with aFolder in theFolders
my ClearEmptyFolder(aFolder)
end repeat
if ((count every item of whatsPassed) = 0) then
-- display dialog "deleting " & name of whatsPassed
delete whatsPassed
end if
end tell
end ClearEmptyFolder
----
And just to comment on why I said that recursion can be a problem with
AppleScript. This is due to the fact that the large number of handler
instances created by recursion can overflow AppleScript's ability to
open new handler instances. I'm not sure of the limits but I have
definitely seen it happen. If it is at all possible use a list instead
of recursion. With AppleScript a list can handle far more elements
than recursion can, in most instances. In this current example the
recursion is very useful because it enables you to check the folders as
the recursion unwinds, that's why I stuck with it even if it might fail
in some circumstances.
- Ken
On Jun 14, 2004, at 7:25 PM, Graff wrote:
You should be careful when using this example script. Although it
does work just fine there exists the possibility that it will fail on
folders that have a deep hierarchy, that is they have a lot of folders
nested inside folders.
However it does help in one matter, it will go directly to the deepest
folders and then back out. So if you delete a folder and that's all
that was in the parent then you can delete the parent also. This can
be a bit tricky with other solutions.
Here's the modified example:
----
tell application "Finder"
set theFolder to (folder of the front window)
my ClearEmptyFolder(theFolder)
end tell
on ClearEmptyFolder(whatsPassed)
tell application "Finder"
set theFolders to folders of whatsPassed
repeat with aFolder in theFolders
my ClearEmptyFolder(aFolder)
end repeat
if ((count every item of whatsPassed) = 0) then
display dialog "deleting " & name of whatsPassed
end if
end tell
end ClearEmptyFolder
----
- Ken
On Jun 14, 2004, at 2:42 PM, Paul Kampu wrote:
Hello all!
Here is a script that very nicely deletes every file from every folder
within the parent, recursively. (don't know who wrote this script)
tell application "Finder"
set theRealFolder to (folder of the front window)
my dumpFiles(theRealFolder)
end tell
on dumpFiles(whatsPassed)
tell application "Finder"
delete every file of whatsPassed
set folderContents to (every item of whatsPassed)
repeat with anItem in folderContents
my dumpFiles(anItem)
end repeat
end tell
end dumpFiles
Now the question is...
Does anyone have some idea as to how to modify this script to delete
the
EMPTY folders only? ie. Leave the folders containing files alone.
Or is it better to start from scratch? If so, any suggestions?
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.