Duplicating, renaming with increment number and archiving the original file?

Hello.

I’m trying to automatically duplicate a file, rename it and move the old file into an archive.
Everything works fine.
Except the renaming part. The original file is named “weekly plan 11”.
I defined a pattern in the rename rule window: /name/_/#/extension/.
First of all it doesn’t recognize the 11 as a number and starts to add 1 to the name. “weekly plan 11_2”.
No matter the setting, it keeps adding up. If I delete the number by hand, it keeps adding +1. So this is my 4th try to edit the settings and it makes a 5. I have to delete the file to reset it to 1. Is there an easy way to define the starting number where it has to begin counting?

I’m not by my Mac, but I think you need to make sure the pattern you’re creating for Hazel to recognize the 11 specifies the 11 as digits.

You may need to “tokenize” the 11 as part of the rule that matches it, then increment that token, then rename the file using the incremented token.

1 Like

Thank you for your reply! Do you know where I could find this function? To tokenize a part of the text? Or do I have to use Applescript for it?

You tokenize within Hazel. I can’t recall if this is an actual term used by Hazel, so you might want to consult the Help documentation.

Please post some screenshots of your rule setup so we can see what you’ve built so far.

1 Like

Hazel definitely uses the term “token”, so that is a good starting point. I just don’t know if “tokenize” is a term the developer uses.

What you want to do is tell Hazel “this section of the file name is something we are going to manipulate later”. You do this by making a matched pattern into a token. The Hazel docs will help you understand how to do this.

The Hazel help docs might even have examples specifically for incrementing numbers using tokens. Incrementing was a relatively recent feature added.

1 Like

Hello! Those are my settings. The name of the file is “weekly plan 11”.numbers but even if I rename it with other patterns like weekly 11 or weekly_11 it wont change anything. I found this tutorial:
https://www.noodlesoft.com/manual/hazel/attributes-actions/action-reference/using-patterns-in-actions/using-custom-attributes-in-actions/

I just have to study it a bit to understand what they mean but I think I will get there. Thank you for your help!

OK thanks. The main issue you have is that you are appending the incrementing number to the end of the filename in your rule. So Hazel is taking the full filename, then adding an incrementing number after it. So your results are as expected, with your rule setup.

You want something more like this:

The number at the end of the filename has been tokenized in my example. Here is the detail on the token:

What I did:

  • add the “Custom Text” item to the pattern criteria to match (this is right after "weekly text " in the example screenshot)
  • click on the Custom Text token, to edit it
  • I named it “number” in the example
  • for the attribute, I clicked on Number ([123] attribute) to add it to the token

This tells Hazel, “match a number, and then save it as a custom token to manipulate later in the rule actions”.

In the Rename action, I set it up like so:

What I did:

  • click in the text field after “with pattern”
  • typed in the text “weekly plan” (you could also tokenize this from the original filename)
  • clicked on the Custom Text token we set up above (named “• number” here)

Next, click on the •number token already added to the rule, so we can manipulate the content

Choose “adjust number…”

And then you can add/subtract:

The obscure aspect is to make that custom token, then set the attribute to a Number. That way, in the rule action, we can call it back, and since we told Hazel it was a number, we have those options to manipulate it.

If you wanted to make this rule more universal, you could also tokenize the “weekly plan” text, similar to how we tokenized the ending number, and just tell Hazel to match characters or words.

Then you could pull that token into the renaming action below. Doing this would make this rule more universal, as it would just try to match patterns like “filename XX”, and they could all be incremented regardless of the filename text. In my example, this rule will be “hard coded” to only match files with the text “weekly plan” following by a number.

But I’d get the number incrementation in place first, then experiment further.

2 Likes

YESSSSS!! It works. You are awesome, sir! Thank you so much for your help. My biggest mistake was to assume that “matches” and “contains” means the same. I think I understand the token thing now. By defining it in the “matches” condition, the rule later knows what is what. :smiley:
Awesome.

Great! Yes the “matches” thing is in no way obvious, and I only know of it after multiple help requests over on the Hazel forums trying to get things to work.

But the “matches” criteria and the tokens can do some very powerful stuff once you get more familiar with them. You can pull data (or should I say match data) from the file content itself (“contents contain match”), tokenize it, and use that to rename and increment or add dates to filenames, reformat them to your liking, even increment dates — all of which is super handy to sort receipts, and other tedious things.

That’s one of my favorite things to do with Hazel, manipulate existing content to rename and sort stuff.

Glad it worked/helped!

1 Like