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
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
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)
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)
cool!
and how about having a defined folder? always the same...
To do that, replace window 1 with something like:
folder "Macintosh HD:Users:nonexistent_folder:"
(71247)
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
@Niel and @kikashi
thanks for your help.
I'm having problem to tell the exact folder...
something like this
- repeat with this_folder in (get folders of "/Users/myname/myfolder/")
or
- repeatwithfin (getfoldersof "/Users/myname/myfolder/")
doesn't work and it reports me error....
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
Try using colon delimiters and putting the disk name first:
folders of alias "MacHD:Users:myname:myfolder"
tw3's solution is very attractive but would still benefit from being made recursive (if your folder structure is likely to warrant it).
Jeremy
Just what I was looking for - works as expected. Thanks for sharing this script.
Script works great except it is identifying .ds files in many of the folders and refusing to delete. Thoughts??
I've made a small and free program that does exactly that:
http://www.macupdate.com/app/mac/52551/find-empty-folders
The advantage of this program is that it also finds folders that are apparently empty but contain the invisible ".DS_Store" file.
And it also lets you move the found items to Trash right away.
Thank you! This still works 5 years later. đ
Susan
delete empty folders