.DS_Store files & empty folders

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

.DS_Store files & empty folders Thu Dec 29, 2022 2:54 am • by eN0ch
I'm exploring the possibilities for a scripted / automated way of deleting, not just individual empty folders, but nests of empty folders. I've been wrestling in my very noob way with Hazel, Automator, ScriptEditor and even shell scripts; and combinations of the above. Nothing seems to do it. The best I've managed this way is deleting the empty folder/s in the bottom layer of the "nest". But nothing will work on the empty parent / grandparent folders.

One idea I've picked up is that it could be to do with .DS_Store files in the folders, meaning the System doesn't recognise the files as empty. So first question: Is there a way to have Hazel delete those files?

Second more general question: Spotlight and Finder seem to me to lack any obvious way of scripting the removal of nested empty files. Is there something I'm missing with Hazel that might get me there?
Lance <º))><

iMac 24" 2021 M1 | macOS 14.4.1 | Hazel 5.2.1
eN0ch
 
Posts: 63
Joined: Tue Jul 01, 2008 7:41 pm
Location: Crookwell, NSW, Australia

Re: .DS_Store files & empty folders Thu Dec 29, 2022 10:37 am • by Mr_Noodle
Can you post the rules you have so far?
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: .DS_Store files & empty folders Sat Jan 14, 2023 3:38 am • by MasterRee
I'm going to hop in on this if you gentlemen don't mind, as I'm working on basically the same issue with .DS_Store files and deleting empty folders.


@eNoch
eN0ch wrote:I'm exploring the possibilities for a scripted / automated way of deleting, not just individual empty folders, but nests of empty folders.


It sounds like you want to run the rules recursively (going through all levels of nested folders). Here is how to do that with Hazel: https://www.noodlesoft.com/manual/hazel ... ubfolders/

The key is to add a rule like this at the end of your list of rules on a folder, which will cause Hazel to apply the top level rules to all nested files and folders (run it recursively).
https://imgur.com/nvRmDQ5


Here is a rule that works wonderfully to delete all empty folders, but I have the same issue that it does not work for folders with that only have a .DS_Store file.
https://imgur.com/aIqbAVc


So to try to resolve that issue I was messing around with this rule that is intended to identify folders that are either truly empty, or that only have a .DS_Store left in them and no other content.
https://imgur.com/2FcpmWy



I'm fine with splitting it in to two rules, one to delete the truly empty folders and another to delete folders that only have a single .DS_Store file in them, but I was trying to do it as a single rule here. I have tried to get a rule to match ".DS_Store" and "DS_Store", using the criteria of name, full name, filename, or extension. I have tried "is", "ends with", "contains", "matches", but no combination of these criteria show a positive match in the rule preview for a test folder with only a .DS_Store in it. I have also tried the above variations with and without Hazel set to scan invisible files
Code: Select all
defaults write com.noodlesoft.Hazel ScanInvisibles -bool YES
.

What is an effective approach to target only folders with a single .DS_Store inside?
MasterRee
 
Posts: 19
Joined: Mon Jan 01, 2018 8:21 pm

Re: .DS_Store files & empty folders Sat Jan 14, 2023 5:51 am • by eN0ch
Thanks for hopping aboard. (And my apologies for not responding to Mr Noodle's invitation at the time).

What can I say? I feel your pain ... I've been on a merry dance with this one, exploring various scripting approaches. (Even asked ChatGPT b :lol: ). I'm very much a noob, you see. I have zero IT training. But I enjoy tinkering with automation, including scripting. Crazy really, but I like it. Well anyway ...

I ended up more or less getting there with a bash script. It runs in two parts. The first part (in theory at least) forcibly removes the .DS_Store files, allowing the second command to then nuke the folders (recursively of course). The two commands are:
Code: Select all
find /path/to/the/folder -type f -path '*/\.*' -exec rm -f "{}" + 2>/dev/null
and
Code: Select all
find . -type d -empty -exec rmdir -p "{}" + 2>/dev/null
.
(Probably should add that I've found reason subsequently to wonder whether I need "rmdir -p" in that second one, or whether "rm -rf" would be as good or better for the folders as well as the files. Or even combine the two into one with "rm -rf"?).

I say "in theory" because I actually don't know for sure what happens, and almost no one else I've consulted (human or machine) seems to either. I get the impression this nested-empty-folders thing is one of the untapped mystery frontiers of coding (or something). That and the mystery of .DS_Store, which few seem to have a grip on. e.g. - do we even know for sure that those blighters aren't like the zombie apocalypse? I get the impression Finder will recreate them on all manner of cues.

At any rate I tried more Hazel rules than I can count, and ended up deleting them all. After ruling out every condition and action my nooby brain could contemplate, there wasn't much left to keep. (And thanks, but yes I have picked up the stuff about subfolders in Hazel. It didn't help with this one, though). Broadly the .DS_Store files seem to remain the big imponderable. And I just drew a blank on how Hazel could manage it. The obvious disclaimer being that as a rank amateur in this field, my judgements don't warrant high trust.
Lance <º))><

iMac 24" 2021 M1 | macOS 14.4.1 | Hazel 5.2.1
eN0ch
 
Posts: 63
Joined: Tue Jul 01, 2008 7:41 pm
Location: Crookwell, NSW, Australia

Re: .DS_Store files & empty folders Mon Jan 16, 2023 10:53 am • by Mr_Noodle
Note that the built-in Sub-file/folder count attribute does skip hidden files, including .DS_Store. It basically only returns the count of visible files.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: .DS_Store files & empty folders Tue Jan 17, 2023 1:02 am • by MasterRee
Mr_Noodle wrote:Note that the built-in Sub-file/folder count attribute does skip hidden files, including .DS_Store. It basically only returns the count of visible files.


:shock:

How did I not know this? That would have saved a few hours of my life. Mr. Noodle, thank you for the tip and for all of the hours that it (and Hazel more generally) will save in my life. I have tested and this rule works perfectly to delete all folder that are empty OR that only have a .DS_Store file inside.

Image
MasterRee
 
Posts: 19
Joined: Mon Jan 01, 2018 8:21 pm


Return to Support