Batch Renaming - Preserve a part of filename

Hi Everyone,

I'm having trouble finding a solution to what I imagine is a simple task for the experts. I would like to batch rename files preserving a set number of characters in the middle of the filename. The filename I'm hoping to preserve always starts after 4 digits and a space. There is a random amount of characters after the part I'd like to preserve.

Example of filenames I would like to rename:
0001 XY12_115_049_0100 - Description of work goes here.jpg
0002 XY12_115_049_0200 - Different description of work goes here.jpg

Example of rename:

I'd like to preserve 17 characters as well as the file extension.

Any help would be greatly appreciated.


Is it always 17 characters? If not, is there always a space directly after the characters you want to keep?

Thank you for your help.

Yes, the part of the filename I want to preserve is always 17 characters. There is always a space directly after the characters.

Here's two versions. The first plays on your "always the 17 characters after the first 4-plus-space" by using a "Substring" action.

Batch Rename Substring.kmmacros (4.1 KB)


The second uses "there's a space before and a space after" to treat the name as a space-delimited array, taking the second element.

Batch Rename Spaces.kmmacros (3.5 KB)


In both cases you can see we're using a "For Each" to process all the items in the selected directory, and the "If" checks that it is a file -- you could add more conditions if you wanted tighter control on what was processed. We split the file path into filename and extension, and finally concatenate the "new" name, a . and the saved extension as part of the "Move" action.

There are many other ways to do this -- I'm looking forward to what others might suggest!

Thank you so much for your quick response! Both macros work exactly as I need them to.

This family of problems occurs frequently enough that there are dedicated apps to deal with it and should be considered if this problem occurs repeatedly in your life.

The KM solutions above are great if this is really a one-off problem, but these applications make things "easier" and "clearer" IMO:

I have used Name Mangler and like it but there are many others.


Plus 1 on Name Mangler. :sunglasses:

+1 for Name Mangler here :ok_hand:

1 Like

Not mentioned yet.

"A Better Finder Rename 11".
Very powerful. Very fast. $30


Thank you everyone for the suggestions. The Macros Nige_S created are working for this specific task, but I'll try out the other applications if I need more flexibility with renaming. I appreciate everyone's help.

I like Name Mangler's UI better for day-to-day stuff, but I've owned a lifetime license for ABFR since the '90s and trot it out whenever I need the big gun.

1 Like