Hazel - Sort Into Existing Subfolder?

I would like Hazel to sort files into existing subfolders based on the file’s name. For example, Hazel monitors a folder called Top Folder. Inside of Top Folder is Subfolder 1 and Subfolder 2. If I put a file named File 1 into the Top Folder, I want Hazel to move the file into Subfolder 1 because the number 1 matches that subfolder. Is there a way to do this? Many thanks!

Sure, this would be fairly easy. It might get tricker if you have 10 sub-folders and you want to match the ‘1’ to ‘1’ and not ‘10’ but even then it would be “do-able”.

Is the number in the filename always at the end of the filename? So

File 1.rtf


File 1 Report.rtf

for example.

Also, will there always be a space before the number?

File 1.rtf




Hazel has options for if the file name contains something or ends with something. Which one I would use depends on how consistently the files are named and how many subfolders you are dealing with.

1 Like

@Tjluoma has the same approach I would take. If you’re familiar enough with Hazel, I’d “tokenize” the matched ending numeral (the “1”) in your example. Do this by choosing “matches” as the rule criteria, and then “Custom Text” for the matched element (and specifying it as a number in the Custom Text setup):

“File [Custom Text Token]”

That would match the “File “ portion and then tokenize the ending digit(s).

That way, you can set the action as Sort Into Subfolder, using the pattern “Subfolder [Custom Text Token]”.

That would have the added benefit of creating the Subfolder if it didn’t exist. And it ensures the subfolder names are exactly that of the ending filename digit.

1 Like

Thanks for the help! In the real world, the folders and the files always start with a five digit code (e.g. 12345 File should go into 12345 Folder), but there is always a space between the number and the word. I think that your method should be revisable though. I’ll let you know if it works!

Thanks for the tip! I’ll let you know if it works!

You all are the best! I’m not sure how to write it out, but I hope the screen shot helps. I match anything with a five digit code, then sort into a subfolder with matching text (the same five digit code, by magic I guess). I very much appreciate the point in the right direction!

UPDATE 1: Spoke too soon! What I have picks up all files that start with a 5 digit code (I added that it has to be a file, not a folder), but it does not always sort into the correct subfolder. I’m having trouble figuring out how to tell Hazel to sort into an existing subfolder with the exact same starting 5 digit code.

UPDATE 2: Got it working. I don’t know why this works, but it works.
To get the file:

  • Kind is Document
  • Name matches Custom Text set to a five digit number followed by Anything.

To sort the file:

  • Sort into subfolder with pattern Custom Text followed by anything.

I put an updated picture below. Thank you again for the help!

I would call this “bucketing” or “sifting”. Pet peeve: Use of the word “sort” for anything other than ordering items. Unless, of course, we deploy “sorted” as short for "sorted out”, as UK English would have it. :slight_smile:

However, generalising this technique would be a very nice thing.

1 Like


Have you seen the Lists & Tables feature in the recently released Hazel 5? Seems as if what you are describing fits in well.

1 Like

@Martin_Packer - Sorry! I don’t know what I don’t know, but I appreciate the lingo tip.
@rkaplan - Thanks! What I have works, but I’ll check out the lists too.

1 Like

Glad it’s working now. I should have pointed out in my example that the spaces I put into my examples should have been included literally.

I see in your screen shot they are not included.

To include a space, just literally type a space character in the rule criteria fields, in-between each blue “lozenge”. Specifically, between “Five Digit Number” and the “…” blue lozenges.

That should help to prevent further false positives. The closer the match pattern is to the exact filename structure, the better. If the 5-digit pattern follows any pattern (like being a date, or always starts with the same two digits, or something like that), including that into your rule criteria will make this more robust.

Same for the “…” (“anything”) criteria. If it follows any patterns, incorporate those in the rule.

trying to wrap my head around this post in addition to coming up with an automated workflow for my use-case.

I have client folders which contains files name with the client’s name and additional file name information, ie YYYY-MM-DD LastnameFirstname Results - Lab (I operate a medical office) for which I would like to automate creation or moving of files to a subfolder.

What I’d like to create is an automation which:
scans the client folder and moves files to another subfolder (either an existing folder or created one if it doesn’t exist) based on wording in the file name

For example:
Folder: DoeJohn

if a file, in that DoeJohn folder, contains “Lab”, I would like that file moved to a DoeJohn subfolder called “Lab” (if it exists or if it doesn’t, the automation would create a “Lab” subfolder)

I can’t quite figure out how to accomplish this

Have you tried using Name contains in your trigger (e.g. Kind is File and Name contains Lab)? You can have Hazel monitor the DoeJohn folder, and if the name of a file in that folder contains the word Lab, the action will trigger. My action assumes that Hazel is moving the file into an existing sub-folder, but I think there are ways to have Hazel create a sub-folder if none exists.

I had thought about doing that; however, as I’ve got nearly 2000 patients, that would be one very expensive rule, and I was hoping to shorten it down with some kind of variable

Ah! Depending on your computer, it may be less expensive than you think. Perhaps you can make a test folder to see if the multiple variable rule works. If it does, then apply it to the big folder. Nothing should be lost even if it breaks (I think - don’t quote me). After the initial batch is finished, ongoing monitoring with frequent triggers is not problematic in my experience.

If your filename patterns are always the exact same:

you should be able to match the filename pattern in a rule, and “tokenize” the strings you want to use for sorting (or new folder creation).

So if the “Lab” string is always after the dash+space string, your rule criteria can match the date, include a space, match the ”LastnameFirstname Results - “ and then tokenize the ending string (I am assuming it isn’t always “Lab”).

Then the action would be “Sort into subfolder”, using the ‘create new folder” blue token (right-facing solid arrow), and for the folder name, use the tokenized matched string (in this example it would match “Lab”).

This way, the subfolder is dynamic, and always sorts into a subfolder named as whatever matches the pattern.

Hazel will not create a new duplicate subfolder if it finds one already there, and will sort into the existing folder.

1 Like