Renaming files using the file name

Hi! I’ve tried to search for an answer, so if I missed a similar solution, please let me know.

In episode 8, the recommendation was to name files with date followed by description. Well, I’ve done the opposite and see the benefit in starting with the date.

So, my question is - If my file names are description-YYYY-MM-DD.ext, is there a way to use Hazel, Keyboard
Maestro (or something else) to batch rename these files to YYYY-MM-DD-description.ext?

I’ve tried this in both applications, but haven’t figured it out. I’m fairly new to both.

Thanks everyone!

Brian

1 Like

Hi Brian! I don’t use either of those applications (yet), but it seems like it would be pretty easy to have one of them do a simple regex matching on the file then, use the matches to rename it. For example:

If you’ve named all your files using the convention you mentioned (description of my file-YYYY-MM-DD.ext) and it’s always 4 digit year, hyphen, 2 digit month, hyphen, 2 digit day, dot extension then you could use something like this regular expression below. However, if you use hyphens between words in your description, then things change a bit. For now, I’m going to assume you don’t.

(.*)(\-)(\d{4}-\d{2}-\d{2})(\..*)

Now, I use group matching because I find it really easy to work with when excluding certain things. There are other regex savants out there that can probably write something more fitting for this scenario, but this is what I came up with and what I would probably do.

Let’s break it down by the groups (notated by the characters in parentheses):
File Name: description of my file-2018-10-15.ext

Group 1 (.*) We’ll call this the description match
This will match anything until it encounters the next group. So, in this case it finds everything before the first hyphen: “description of my file”

Group 2 (\-) This is a throw-away group and we won’t use it
This will find the first hyphen after group 1’s match up until it encounters the next group: “-”

Group 3 (\d{4}-\d{2}-\d{2}) This is the date match
This finds your date by looking for exactly 4 digits, a hyphen, exactly 2 digits, hyphen, exactly 2 digits: “2018-10-15”

Group 4 Not sure if you need this, but it’s the extension match
After all that group matching up there, we’re left with only 1 thing. The extension (includes the dot): “.ext”

If Hazel or KM uses zero based regex, then your grouping would obviously start at 0 and you’d skip the 1st group instead of the 2nd group.

I really hope this helps you. Please feel free to ask any questions you’d like. But, my guess is someone is going to chime in and say "Oh, Hazel/KM can do that very easily by just clicking here, or rubbing that magic lamp there (as is the case usually from what I hear).

Cheers.

1 Like

Thank you fischgeek! I think I understand what your stating… I’ll digest this and see if I can apply this logic to KM/Hazel. Always love the challenge.

I use Better Rename 10 for similar tasks.

2 Likes

If the file names consistently end with the formatted date, why not just grab the last 10 characters of the file name rather than the elaborate pattern match?

I see your point, but it would actually have to be the last 14 characters. Provided the extension was always 3 letters which we can’t guarantee.

The regex way will always get the date regardless of the extension.

I suppose you could split on the dot but you also can’t guarantee those won’t appear in the file name itself.

Regex just seemed like the correct tool for the job. But there are many ways to do it. Pick your favorite!