Recursively delete empty subfolders from a folder

I’m looking for a quick way to delete all empty folders in a given destination, using NSMetadataQuery.

I thought kMDItemFSNodeCount might be a good candidate for this, but it seems to take invisibles into account.
The point is that when requesting folders with a node count of 0, it will ignore those that have a .DS_Store.
And if you ask for node count 1, then folders that don’t contain invisibles become the problem.

Any suggestions will be appreciated.

BTW, there’s an app for that: “Find Empty Folders” on my site:

Unless you really want to write your own script for the experience? You cannot use mdfind for this but have to read the entries of every directory, checking their names, and count all that are not named “.DS_Store”. Also: If you cannot access a file or a folder content, it means it’s protected, and you need to not delete their parent dir.

Hi Thomas.

I know your app but as you guessed, this snippet is part of a more complex script.
So, if I understand well, the only way to do this is to loop through every subfolder?

FYI, this would be pretty easy to do using Shane’s FileManagerLib script library.

If you don’t want to use it as a library, you can open the script in your editor and “borrow” the code used.

The process using the library would be:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "filemanagerlib"

set myPath to path to documents folder

set allFolders to objects of myPath ¬
	searching subfolders true ¬
	include folders true ¬
	include files false ¬
	result type files list
repeat with thisFolder in allFolders
	set thisFolder to thisFolder as alias
	set subFolderItemNames to objects of thisFolder ¬
		searching subfolders false ¬
		include folders true ¬
		include files true ¬
		result type names list
if subFolderItemNames is {""} then
		remove object thisFolder  with folders included
	end if
end repeat

Thanks Ed,

I end up with the same solution but all written in AppleScriptObjC.
Will post it tomorrow (it’s about midnight here).


1 Like

Here it is.

use framework "Foundation"
use scripting additions

set theFolderURL to current application's NSURL's fileURLWithPath:"/Some/folder/path"

-- get all items in folder descending into subfolders, ignoring invisibles and package contents
set theFileManager to current application's NSFileManager's defaultManager()
set theContent to (theFileManager's enumeratorAtURL:theFolderURL includingPropertiesForKeys:{"NSURLTypeIdentifierKey"} options:6 errorHandler:(missing value))'s allObjects()

-- getting all subfolders
set thePred to (current application's NSPredicate's predicateWithFormat:"pathExtension == ''")
set theFolders to theContent's filteredArrayUsingPredicate:thePred

-- deletes every subfolder or any "tree structure" of subfolders that contains no files
repeat with aFolder in theFolders
	set subContent to (theFileManager's enumeratorAtURL:aFolder includingPropertiesForKeys:(missing value) options:6 errorHandler:(missing value))'s allObjects()
	set thePred to (current application's NSPredicate's predicateWithFormat:"pathExtension IN %@" argumentArray:{"scptx"})
	set fileCount to (subContent's filteredArrayUsingPredicate:thePred)'s |count|()
	if fileCount = 0 then (theFileManager's removeItemAtURL:aFolder |error|:(missing value))
end repeat