Delete empty folders

HI,

I’m trying to figure out a way to delete all empty folders within a selected folder. I found a script that worked in OS 9, but it’s not working now with the .ds_store or some other issues. I’m on 10.4.6 btw.

Here is the script in question:

tell application "Finder"
	delete (every folder of entire contents of selection whose size is 0)
end tell

Any help greatly appriciated.
TIA
Jeff

Hi,

It’s very easy to check the size of a folder. When I run the following and choose a new empty folder:

set f to choose folder
tell application “Finder”
size of f
end tell

I get ‘missing value’.

gl,

That part doesn’t work for me.

Edit: I’m also trying to do this with a shell script, but I don’t have it quite right yet.

Hi,

I’ve been getting different non-zero values using ‘size’ on an empty folder. It might be better to count the items:

set f to choose folder
tell application “Finder”
set temp_list to items of f
count temp_list
end tell

gl,

Kel,

Does this mean I should change zero for “”?

Like
delete (every folder of entire contents of selection whose size is “”)

In the mean time I’ve tried the following, and it still doesn’t work, but might give you some other insight as to what I’m trying to do.


set the_container to (choose folder)
tell application "Finder"
	set container_list to (folders of container the_container)
	repeat with the_sub_container in container_list
		set the_sub_container to (the_sub_container as string) as alias
		set theItem to item 1 of the_sub_container
		if number of file of theItem is 0 and number of folder is 0 then
			delete theItem
		end if
	end repeat
end tell

Jeff

The size of an empty folder (whether it had something in it before or not) is “missing value” without the quotes.

Adam,

I’ve tried your suggestion but still get an error about some data could not be transfer into proper type or something.

I’ve included a try statement to eliminate the error just in case it was stock on something for nothing. But the script still didn’t do it’s thing.

tell application "Finder"
	set the_folder to choose folder
	try
		delete (every folder of entire contents of the_folder whose size is missing value)
	end try
end tell

Jeff

Hi Jeff,

What I meant was, don’t use size because as you noted, there could be invisible files in a folder. It’s unreliable and I think it’s never zero.

You might need to go through every folder and check if it’s emty unless there’s a unix command that can delete empty folders.

gl,

This one is kind of tricky, because as you delete folders, their parent folders may become empty. So, you need to think about this a little.

gl,

Hey, thanks Jacques, It works!!! Very cool:-)))

Jeff

And, Jaques, has the wonderful safety margin of putting them in the trash which a do shell script "rm … " wouldn’t do.

Adam, the safety is off in my case since all files are on a server, which means, they get deleted permanently without going to the trash folder.

Jeff

I believe you could move it to the trash (~/.Trash).

Perhaps the OP could move them to his trash too. That won’t take long if they’re empty, but at least he’ll see what was empty.