Help Me Rename a Group of Files Please

Hi -- I have a mental block when it comes to regex :upside_down_face: and can't figure this out for the life of me.

I'd like to be able to choose a group of files in the finder and then run a KM macro to change their names based on user input and the current month:

Existing file names:

10536-MARBLE-Jun-BC-77.txt
10536-SLATE-Jun-BC-72.txt
10536-COPPER-Jun-BC-58.txt

Desired file names:

10987-MARBLE-Jul-BC-77.txt
10987-SLATE-Jul-BC-72.txt
10987-COPPER-Jul-BC-58.txt

I'd input the new code number (10987 in the example) and the macro would calculate the date. Delimiter in the file name is always a dash.

I've been looking at SPLIT PATH to get the file name, but then I get caught up in the correct regex to split and substitute.

Any help would be much appreciated.

Thank you,

Randy

I would reach for this:

(the fiddly character of regular expressions makes them risky in the context of file names)

2 Likes

I too use a dedicated App for batch file renaming. There are so many possible patterns to batch renaming files that it seems like it's a perfect task for a small App that has been written to do only that task (and gives you the chance to preview before committing to the changes). For years and years I have been using A Better Finder Rename which is now on version 11.

https://www.publicspace.net/ABetterFinderRename/version11.html

3 Likes

:+1:+1 for Name Mangler. Itโ€™s a great utility but it really shines when renaming batches of thousands of files which it does somehow in a few seconds!

You can do this without using regular expressions.

If you get the name of a file into a KM variable. For example set fname to 10536-SLATE-Jun-BC-72 then you can treat the variable as a KM Array Variable with a custom delimiter.

In this example %Variable%fname[1]-% will return the value 10536
and %Variable%fname[2]-% will return the value SLATE
and so on.

You can then reconstruct the file name that you want by stringing together the date fragments with the bits you want to keep. For example:

10987-SLATE-Jul-BC-72

Is formed from

10987-%Variable%fname[2]-%-Jul-%Variable%fname[4]-%-%Variable%fname[5]-%

As Iโ€™m not at my Mac right now I canโ€™t actually give you an actual macro to download, but hopefully this gets the idea across!

PS: you can read about variable arrays with a custom delimiter in the KM wiki: manual:Variables [Keyboard Maestro Wiki]

3 Likes

In addition to all these more than adequate replies, let me point out there was a discussion about this a while ago that led to both Perl and JavaScript Keyboard Maestro macros. You can specify a regex to parse the existing selected file names and you'll see a preview of the change, which you can sign off on or modify on the fly. I've used the Perl version since.

The prompt (with the regex for your case) would look like this:
ss-408

And you would see this for confirmation:
ss-406

1 Like

I bought Name Mangler last month and like it a lot, but I often need to select files in the Finder and rename them quickly and I find that opening a separate app and entering in a bunch of specific info for finding and replacing slows me down.

Tiffle, that sounds very promising. Thank you. I'll have to play with that to see if I can make it work. Certainly looks easier than regex!

1 Like

You can automate a lot:

  • with Name Mangler's Name Conversion Description Language (NCDL) (See Help > Advanced)
  • and by saving Presets (See Presets > Managing Presets)

I'm back at my Mac so I thought I'd help out by posting an example macro for you. It will do what you've asked but, as with any macro supplied to you by someone else, you need to test it on some files that are copies/not important just to verify it does what you want it to.

You start by selecting the files of interest in Finder and then trigger this macro. It will then prompt you to enter your "code" and the date will be set to the current month - which you can change if you wish.

I've used Local variables throughout which then get deleted when the macro finishes - thereby avoiding clogging up your installation of KM with unwanted variables.

For safety, the macro is uploaded in a disabled state.

Test Rename Files Selected in Finder.kmmacros (4.5 KB)

Click to see macro

BTW - looking at how the new filename is "constructed" in this macro makes @mrpasini's suggestion quite attractive :grinning:

Thanks very much Tiffle. I actually figured it out last night and I will compare my solution to yours. This is what I am working with so far:

1 Like