Help w/ RegEx & File Rename


I'm trying to script a file rename macro using a RegEx expression. I've tested the RegEx in Visual Studio Code, and the expression matches the test text:

RegEx: ^(\D+)(\d\d\d\d).+$
Test Text (typical file name): Galaxy Quest 1999 1080p BluRay Backup Complete

But I keep getting an error in Keyboard Maestro that the RegEx failed to match. Here's my script so far. (Note in the file name, the spaces were originally periods.)

Any help would be appreciated!

Please post your macro so we can test it ourselves. File > Share macro to forum > copy the uploaded URL into this thread. (Or something close to that at least - you’ll work it out)

It doesn't look like you ever put the parent and base parts back together to create %Variable%VarName% upon which the regex search is presumably supposed to take place.

Thank you for the replies! I uploaded the macro in question to a new thread.

Hey Tom,

You're doing some of this the hard way.

See the Split Path action.



1 Like

Thanks, Chris!

1 Like

Moderators Note

Posts #7 through #10 moved here from a separate topic – 2022/03/18 18:28 CDT – ccs

Hi All,

I started a previous topic but didn't know you could upload a macro to the forum from KM.

The RegEx expression I came up with is just to search the file name, separated from the file path and the file extension. I put the path, file name, and extension back together in the last step of the Macro.

The problem I'm having is that although the regular expression works on test text when I use it in Visual Studio Code, it is not working in Keyboard Maestro for some reason.

Typical file names are:

And I'm trying to rename the files, e.g.:
Galaxy Quest (1999).mp4
Viva Max (1969).mp4

Help w/ RegEx & File Rename

Test RegEx Rename.kmmacros (4.5 KB)

As noted in the other thread, your regex search is being run on a variable named "VarName" , which doesn't exist in this macro.

1 Like

Ok, well, file under the heading of "Duh!"

Thanks for the help!

Incidentally, there's no need to create a separate thread when you upload a macro – just copy the tags in the draft new topic created by File > Share > Keyboard Maestro Forum

(which look something like this):

<a class="attachment" href="">Some macro to upload.kmmacros</a> (3.5 KB)

![Keyboard Maestro Export|454x778](upload://t8JUsiJrpPQKCJO2Lkx2PO53mys.png)

and then:

  • cancel and delete the draft new topic
  • paste the copied tags into the existing thread.

Reduces noise, effort, and confusion :slight_smile: