Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sealdriver

macrumors newbie
Original poster
Hi
I want a script that will delete any empty folders of a given folder. I realise that trying to make it checked nested folders may be very difficult so I started by just deleting the empty folders at the top level. However, my script doesn't work. as a complete beginner I hope someone can point me right.
Ideally I would like the script to ask me which folder to check when it first runs but I was getting totally lost with that. Here's what I have


tell application "Finder"
set folderPath to "/Users/Pat/Desktop/Folder1"

repeat with checkFolder in (get folders of folderPath)
if (count items) of checkFolder is 0 then delete checkFolder
end repeat
end tell


Thanks
 

pmau

macrumors 68000
Nov 9, 2010
1,569
854
Hi
I want a script that will delete any empty folders of a given folder. I realise that trying to make it checked nested folders may be very difficult so I started by just deleting the empty folders at the top level. However, my script doesn't work. as a complete beginner I hope someone can point me right.
Ideally I would like the script to ask me which folder to check when it first runs but I was getting totally lost with that. Here's what I have


tell application "Finder"
set folderPath to "/Users/Pat/Desktop/Folder1"

repeat with checkFolder in (get folders of folderPath)
if (count items) of checkFolder is 0 then delete checkFolder
end repeat
end tell


Thanks

Terminal

find . -type d -empty -print0 | xargs -0 rmdir

Much easier.
 

pmau

macrumors 68000
Nov 9, 2010
1,569
854
Thanks for that but would that not simply delete all empty folders on the disk?
That's not what I wanted to achieve.

Cheers

It starts from where you currently are. Note the ".".
If you change the directory to "startHere" than it will start there ;)

If that's not what you meant, I sincerely don't understand your question.

You want to recursivly delete all empty folders under a certain folder, right?
 

pmau

macrumors 68000
Nov 9, 2010
1,569
854
Hi

Thanks, I hadn't realised it started from your current location, I assumed it would just sweep the disk for empty folders.

Cheers

Oh, one last comment.

If a folder gets empty because its last entry was an empty folder, it might not get deleted. Do do that, find needs to descend into folders first.

This would be done adding the "-d" option ...

Cheers
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.