Autorename file independent from last file


I often have pdf files with the following structure:

here some real examples:
and so.

In this kind of groups almost every month a new file will be added.
So, I’m searching for a script for automatic renaming. Looking up the last file name. Tage the next month, append the rest of the text and finish renaming.
As example. We assume I have a group and the last filename is: 2020-01-31_RE-account-TD.pdf
Now when I add a new file, I will have it renamed to 2020-02-28_RE-account-TD.pdf

I this is too complex, it would be also helpful. If the part of the date only will be renamed like this:
2020-01_RE-account-TD to 2020-02_RE-account.TD

Any hints or examples?


I’m not sure if I understand your posting correctly. What I gathered seems to be:

  • you have PDFs for every month
  • the naming convention for these PDFs is yyyy-mm-dd_something.pdf with dd being the last day of the month.
  • If a new PDF of kind ‘something’ arrives, you want to take the date part of the last ‘something’ PDF and ‘somehow’ determine the next matching date - i.e. the last day of the following month.

Assuming that this is what you want: what’s the point in referring to “the previous something” here? If your document belongs to a certain year/month, this date can surely be determined by looking at the document itself. Otherwise, how would anyone know where it belongs? Or why would it even be in month x vs. y? In your example, the gas, VAT, mobile documents already should have an inherent date. Using that seems natural and keeps you sane.

The alternative would be scripting: gather all the ‘something’ records, sort them, take the last one. Split the first part of the name in yyyy, mm, dd. If the mm is 12, increase yyyy by 1 and set mm to 1, dd to 31. Otherwise, increase mm by 1. If mm is 2, figure out if yyyy is a leap year (divisible by 4 but not 400). If so, set dd to 29, otherwise to 28. For all other months, look up the last day in an array. Reassemble the date and build the final file name.
This can certainly be done in AppleScript, on a long lonely winter night. Or in a bright half hour in JavaScript. Regardless of the language: why?

first, sorry for my late answer, I was of for some days because off illness.
Yes, you got it, what I want to do and, yes, you’re right, in every document is some kind of information for which month and year the document belongs.
But, and this is the reason for that post, this is totally different for every document. So it would be necessary to write for EVERY document an own smart rule.
When using my approach you have the benefit to have one script do it for all relevant documents.
But, never-the-less, thanks for your explanation, it seems that there is no easy way. I already tried to figure out the right year and month from some documents. But this is also very time consuming and in same case not easy.

Not necessarily: DT already provides several date placeholder, e.g. newest and oldest document date (cf. the documentation on “placeholders”). You could add one of those to the name in a smart rule.

Yes, I already know these placeholders but in documents like a Bank statement with a set of dates in the document, you get some wired values in the placeholder, but not what you expect. So these placeholders only works about 50% of my cases.
So, this was my idea to find a smarter solution. But it seems there is no smarter way.

Using the oldest or newest document date instead of the automatically chosen one might help in these cases. Or you could add a Scan Text > Date action first to specify a prefix and/or suffix to find the right date.

Yes, I know this, but at the end this means I must maintain a set of smart rules to get every case handle. And that means more work for me than if I rename the documents manually. I want to avoid having to maintain several smart rules.

That will be difficult to avoid. I have Hazel rules for my bank accounts and televom bills – one for each bank and telecom provider (funny enough, it’s only one provider, but they send out mobile and land line bills in different formats). In some of these cases (mostly bank statements) I can deduce the month/year from the file name.

So either you have so many documents to sort that having rules for them is worth the (mostly) one-time effort. Or you just can do it manually anyway. That’s mostly a question of your personal pain points.