Editing A File Name With Hazel

I often send to myself (don’t ask but it’s from a mainframe and no reason to snigger on that either) data sets which get saved to disk on my Mac as files whose names are of the form

PACKER.HMLR1019.x.y

And I’d like Hazel to automatically rename them to

x.y

Actually the parts before the second dot aren’t constant. The folders within Documents aren’t constant, either. Indeed the HMLR1019 is a subfolder of ~/Documents/Customers/HMLR - most of the time.

Can Hazel do this? If so it would save a manual step in my workflow that is tedious - particularly if there are multiple such files appearing at once (which there often are).

Having had Hazel for a few years this is actually my first practical use case. Shame on me. :slight_smile:

A few clarifications that could affect how you might do this.

Are you saying that the files to be renamed by Hazel are always in sub folders of ~/Documents/Customers/?

  • Is y the file extension or is it part of the base file name and there is a further extension of some sort?

Good questions.

  1. It is always a subfolder of ~/Documents/Customers/ but for different customers the next one will obviously be varying.

  2. The y is the extension. Generally it is csv but sometimes I might want to rename .SCRIPTR to .csv. (That would be a stretch objective.)

Let’s create three rules in Hazel for the ~\Documents\Customer\ folder.

RULE #1: Base Rename

Use Hazel’s pattern matching and custom text match items to match the three file name parts separated by periods. The examples are identically defined alphanumeric parts, so I’ve applied explicit alphanumeric matches.

The rule drops the first and second custom parts and simply outputs the third and the extension.

RULE #2: Rename SCRIPTR Files

This rule simply renames *.SCRIPTR files to *.csv.

This rule will run after the first rule due to the sequencing of the rules.

RULE #3: Go into Sub Folders

This rule matches all folders and runs the group of rules against them. This in effect gives the rule set a recursive folder action where it applies the rules to all sub-folders of the Customers directory.

In effect rules 1 and 2 are run against every older in the tree beneath Customers.

You can either build the rules yourself, or download and examine my test set.

I gave it some quick testing and it seemed to work as I expected, but if you examine the rules, backup your data and then give it a test, hopefully it’ll do what you are after.

Hope that helps.

1 Like

Thanks for this. Getting it working taught me a lot about Hazel.

1 Like

How can I have Hazel drop a space from the beginning of a name? In my case I want to rename “# filename” to “filename”

Thanks!!!

If you have a new question, it is best to post it as a new topic. This makes topics easier to search for and also keeps the original topic on topic.

This discussion thread includes discussion of breaking a file name into multiple parts and using just a subset of those parts. You could apply this to your scenario by making "# " one of the parts and then dropping it.

Alternatively, you can use the “Rename” rule and click on the little arrow to the right of “name”, then select the “replace text” option and replace # with nothing. :slight_smile:

1 Like

I’m not at my Mac and don’t write Hazel rules regularly enough to recall, but does the replace text replace all instances, or accept regex to specify a pattern?

The removal technically should only occur once and only at the start of the name.

No, you’re right, this does assume it only occurs once at the start of the file name.