delete empty folders

i wrote this script to find empty folders and delete them. I want to know:
how to delete folders which are not empty but only have “.DS_Store” file in them

--script to find empty folders and delete them
set pathoffolder to alias "Leopard:Users:lance:Desktop"
--change folder here
tell application "Finder"
	
	set foldernames to name of every folder in pathoffolder
	repeat with aItem in foldernames
		
		get size of folder aItem
		set s to result
		--s stores the size of folder
		if s = 0 then
			move folder aItem to trash
			
		end if
	end repeat
end tell

Hi,

the Finder (unlike System Events) doesn’t consider invisible files
try this


--script to find empty folders and delete them
set pathoffolder to path to desktop
--change folder here
tell application "Finder"
	repeat with oneFolder in (get folders of pathoffolder)
		if (count items) of oneFolder is 0 then delete oneFolder
	end repeat
end tell

your logic seem right (i mean, count item “0”) but it does not work

also could you plz tell me: why the script i wrote works only when i execute it twice

Hide the invisible files, then it works

It takes some time to calculate the size of the folders.
The second time it works, because the sizes are valid.

thanks a lot.
yes, it does work…but i generally keep all files visible.
is there a way to hide invisible files through the script and make them visible after the empty folders are deleted

yes, but you have to restart the Finder after changing the invisible property
try this instead


--script to find empty folders and delete them
set pathoffolder to path to desktop
--change folder here
tell application "Finder"
	repeat with oneFolder in (get folders of pathoffolder)
		if (count items) of oneFolder is 0 or ((count items) of oneFolder is 1 and name of item 1 of oneFolder starts with ".") then delete oneFolder
	end repeat
end tell

thanks…i will have to be cautious about files that start with “.” -----i mean in external drives. like ipod or my ceillphone’s memory card…they all have some important invisible files with “.”
but mostly the count would be more than 1 in those cases

what’s about this, it deletes the folder if there is only one item .DS_Store


.
  if (count items) of oneFolder is 0 or ((count items) of oneFolder is 1 and name of item 1 of oneFolder is ".DS_Store") then delete oneFolder
.

yes that should be good enough
Many many thanks StefanK for “real-time” help