A Hazel File Rename Rule

I plan on writing a post all about Hazel in the future, but I wanted to share a specific use case for Hazel that others might benefit from.

In short, Hazel is a very cool macOS tool for automating the moving and naming of files. I know that does not sound very fun, but it is incredible. Again I will talk more about that in the future but let me show you what I wanted to do:

My File Structure for My HBS Classes

The screenshot above is a branch in the file tree for my Human Body Systems class. As you look at this branch from left to right in the image above, you can see that I have multiple folders for Quests (what other teachers would call a unit). Inside of each Quest folder are folders for Mini-Quests. Mini-Quests a series of lessons that are closely connected via their content. Lessons are pretty self-explanatory, but each lesson has Assets, which are the files I need to teach the lesson. In the example above, I have three Assets: a spreadsheet, a presentation, and a handout.

Being who I am, I wanted to unify the naming of all of my files. Ostensibly to make searching better, but really just because I was procrastinating and organizing things make me feel good! Additionally, I have been on an automation kick recently, so I wanted to figure out a way to automate this organization, and Hazel was precisely the tool for the job!

Renaming Files Based on Their Parent Folders

I quickly figured out that Hazel can rename files and use the name of the folder that the file is in as part of the new name. Since my folder structure was pretty sharp (if I do say so myself) this was the rule I needed:

My File Renaming Rule

The above rule will take any file (not folder) and prepend the file’s name with the name of the folder the file was in and prepend that with the initialism HBS (the name of my class). This rule also will not run on a file that starts with HBS to prevent it from infinitely renaming files.

In the example from the top, Hazel took my file “Student Response Sheet” and added the name of the folder that file was in “1.2.4 Height Estimation from Bone” and added HBS to the front of it all to rename the file “HBS 1.2.4 Height Estimation from Bone- Student Response Sheet”. I was delighted with this!

Stumbling Block

The downside of this rule is that I would have to add it to every folder in my structure (which is a lot). So I started looking for a way to make my renaming rule work on lots of folders, including ones I have not created yet. Enter the “Run rules on folder contents” action!

Run Rules On Folder Contents

This rule looks very simple, and it is, but I could not get it to work. I added the “Run rules on folder contents” action to my HBS renaming rule, and nothing was happening. I was puzzled.

I read an article on the Hazel support site, which said I just needed this rule by itself, and everything would be ok. I tried that and still no luck. Until I realized the order of the rules matters:

My Rules for “01- Quest 1- Incredible Identifier”

It turns out that not only did my Prefix File Names rule and my Go Into Subfolders rule need to be separate, the Go Into Subfolders rule also needed to be at the bottom of the list (I still don’t understand why). Only then would Hazel to go into every Mini-Quest and Lesson folder and rename the files at the end of my file structure.

Once I reordered the rules, everything worked like a charm, and my files are very nice and tidy!

Conclusion

As a bonus, I am doing this on folders that are synced to my computer via Google Drive so I can add a file to Google Drive on my phone, and Hazel would give that new file a proper name just like everything else!

2 replies
  1. Caleb
    Caleb says:

    Super helpful here. Had no idea you could click within the field and just type the preferred appended name. Thanks for sharing this, you saved me a ton of time.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.