delete empty folders

I have one main folder with many other folders inside.

I'd like to delete empty ones. Is there a way to do it?

thanks

Posted on Nov 1, 2012 3:08 PM

Reply
11 replies

Nov 2, 2012 2:01 AM in response to personal_username

You might want to try a recursive solution, so that empty folders containing only empty folders are deleted. Something like this:


on killEmpty(fol)

tell application "Finder"

repeat with f in (get fol's folders)

my killEmpty(f)

end repeat

if (count items of fol) is 0 then delete fol

end tell

end killEmpty


tell application "Finder"

repeat with f in (get folders of window 1)

my killEmpty(f)

end repeat

end tell

Nov 2, 2012 6:03 AM in response to personal_username

You'll find that doing it this way is much faster:


set folderToClean to "/path/to/folder to clean/"


tell application "System Events"

set emptyFolders to path of folders of folder folderToClean whose (disk items whose visible is true) = {}

end tell


tell application "Finder"


deleteemptyFolders


-- empty trash


-- uncomment the above line if you want the trash to be automatically emptied

end tell

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

delete empty folders

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.