Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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
Question marked as Best reply

Posted on Nov 1, 2012 3:13 PM

Run this AppleScript when the main folder is in the frontmost Finder window:


tell application "Finder"

repeat with this_folder in (get folders of window 1)

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

end repeat

end tell


(71245)

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

delete empty folders

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