Macro request: Add dashes after 4th and 6th character of filename

I’m trying to create a macro that can rename selected files in Finder by adding a - after the 4th and 6th character of the base filename (before the extension).

Example of what I want:

  • Original file: ABCD1234.txt
  • Renamed file: ABCD-12-34.txt

Could anyone help me achieve this?

Here is a regex way:

Search for ^(.{4})(.{2}) (group 1 contains the first 4 characters; group 2 contains the next 2 characters) and replace the match with $1-$2- .

1 Like

Could you explain how the "selected files in Finder" aspect of my request works into this Macro?

Which step here executes a rename of the filename(s)?

That part can probably be left as an exercise for the reader

(see the action described in the Keyboard Maestro Wiki here: Rename a file )


On the file name change which @kevinb made a start on for you,
what would you want if the 5th and 8th characters were already hyphens ?

  • no change ?
  • additional hyphens ?

Hm, will take a look! To answer your question, "no change" probably makes the most sense here.

I'm still having trouble implementing this. Could someone show how to incorporate this into a full macro?

Could we see what you have for your macro so far or else a list of the overall steps that you think the task could be broken down into? Then it will be clearer where you need help.

I’m sure that someone would gladly offer a complete working macro if it’s needed, but it might not do what you expect it to (besides @ComplexPoint’s question, what other ifs and buts might need to be thought about?) and a “full macro” might cheat you of some useful experience based on your own ideas.

Sure, though I feel the title of the post captures it well: I am intending to add dashes to filenames. The context is dates -- I have a string of files with date listed out (e.g, 20250617) at the start of the file. I'd like to batch select these and be able to quickly convert their names to 2025-06-17.

So, the steps as I would see it, are:

  1. For each item in a collection execute actions (Finder's selection)
  2. Add a '-' before character 5. and a '-' before character 7.

and where the filename is short ?

(so that character 7 and/or character 5 either don't exist, or are part of the file extension ?)


this.txt → ?

or too long? (255 maximum file name length)

Then the macro as intended would not work, but I am not intending to run this on filenames that don't start with yyyymmdd (why would I?) so I think this is a non-issue?

When a macro does something to files selected in the Finder, sooner or later it will be inadvertently applied – for example to an edge case at the end of the selection, or an anomaly within it, or simply applied twice to the same files by accident.

(Not worth risking unexpected side-effects to files)

Copy - For filenames that are too short, perhaps the macro could detect this and send an error alert?

Obviously, longer filename lengths will vary as I typically have text beyond the yyyymmdd at the start - there is no consistency here. However, an error could also be worked in for a 255 max file name length scenario.

Thoughts?

Alright then… Here is one way…

The For each action operates on “the Finder’s selection”, on the assumption that you will want to select the files first, before running the macro. If you had another workflow in mind (e.g. renaming all files in a given folder), you would have to modify the macro.

Notes:

  • The items making up “the Finder’s selection” are not just the names of files (e.g. “ABCD1234.txt”) but the names of paths (e.g. “/Users/KeyFinder/foo/bar/baz/ABCD1234.txt”.
  • The Rename file requires a full path to work on, but for the new filename we have to supply it with just that new filename, not a full path.
  • I have tweaked the regex in the Search and replace action so that it will match everything in the path up to and including the final slash (“/”), followed by the regex to match the filename (which is as detailed previously).

Rename files.kmmacros (2.8 KB)

The points that have been made about checking for the lengths of filenames and so on are of course worth considering, but not, in my opinion, until after one has managed to put a basic macro that does the job. This demo macro performs no error checking.

Before running your macro (this one or any other): make sure that everything is backed up; test the macro only on disposable files; try to break the macro with invalid inputs—or, if are confident that you can live with the consequences and that you know what your macro is doing, forget about perfectionism and test-driven development and just wait for something to break and then you can fix it..? It’s your call, according to your needs.

1 Like

This works perfectly! Thank you!

1 Like

This doesn’t at all match the input example that you gave (that started with letters rather than date numbers).

A slight adjustment to the regular expression, taking the late-mentioned yyyymmdd pattern into account, would allow for leaving the file name unchanged when it already contained hyphens, or was simply too short, etc