PDA

View Full Version : Delete empty folders




sealdriver
Feb 19, 2012, 07:24 AM
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
Feb 19, 2012, 07:26 AM
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.

sealdriver
Feb 19, 2012, 08:52 AM
Thanks for that but would that not simply delete all empty folders on the disk?
That's not what I wanted to achieve.

Cheers

pmau
Feb 19, 2012, 08:58 AM
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?

sealdriver
Feb 19, 2012, 09:02 AM
Hi

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

Cheers

pmau
Feb 19, 2012, 09:07 AM
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