I would suggest Hazel, which you already own. Note that I just checked this in Hazel 5, which I installed tonight. I don’t recall if this can be done in Hazel 4.
You can create a rule matching ALL of the following. Make the first element that the “kind” is “Folder”.
If you then option click the plus button to add a new rule, you get the “subrule” construct. In the subrule, on the first line of it, you can read "If of the following conditions are met for ".
When you click the dropdown, you can it to “any of its subfiles or subfolders”. Add the condition in the sub condition to “name contains 5077” or “contents contains 5077” depending on which you need, AND that the kind is PDF.
This should match any folder that contains a file or subfolder that is a PDF and has 5077 in it (or in its name depending on how you need to create the rule).
For your action you can do whatever you want to accumulate these folders, eg apply a tag “5077” to the folder.
Then you can use Spotlight to find all of the folders tagged with 5077, or create a saved Finder SmartFolder to keep track of them.
Tell Hazel to run the rule to get everything populated, and of course apply this rule to the top level folder than contains all of the folders you need to search.
Note that there is a hitch to this: if you have a PDF two subfolders deep, every folder “above” that PDF is going to get the tag. It’s much harder if you only want to tag the top level folder of each tree.
In that case you might need to play with extra criteria in Hazel to ensure that a folder is NOT a subfolder before tagging it. I have’t played with how to do that in Hazel easily.
If all of the folders you want to do this for are under one top level folder, let’s call it “A”, then you could do something in a command line construct, sort of like:
for f in A/* # Go through everything in folder A
do
if [[ -d $f ]] # Only process if this is a subfolder of A
if [[ $(find $f -type f -name ".pdf" -exec grep 5077 {} \; ) ]]
then
# This folder - $f - contains a pdf that has 5077 in it
# use a utility like tags to add a tag to the folder $f, or use xattr to write the tags
fi
fi
done
Please note that I HAVE NOT tested the command line approach in detail and there may be some syntax errors, I just typed it straight into here. If you want to pursue that approach make sure it works on a test folder (like a copy of the folder tree you want to use it on) before turning it loose on real data!! Obviously it identifies pdfs by having a .pdf extension (but not if it’s .PDF!) so you might have to tweak to suit your actually data.
You could replace find with mdfind, which will be faster and you can use mdfind easily with the filesystem metadata to find pdfs, BUT mdfind only finds files that have already been indexed with Spotlight, and so can miss unindexed files - which might include anything recently created. If this is something you will only run once or infrequently, I would go with find which is slower but more comprehensive as it scans every file in the provided folder.
Hope this helps.