Batch Rename Files with Numerical Prefix Ascending by Date Created

Hi there! I just made the move over to Keyboard Maestro from QuicKeys and I have managed to re-do most of my scripts that I had working in QK apart from one for batch renaming files in a particular way.

The script I used to have would order the Finder selection by date created and add a prefix 01,02,03 etc. This was very useful and I would love to re-create it in KM.

I'm struggling as the rename files macro doesn't have the same amount of preset options available as QK..

Help appreciated.

Many thanks!
Martin

Hi @catherinesongs - welcome to the KM forum.

There is an example in the KM wiki that is very close to what you’re trying to do; it’s entitled Working with the Finder Selection and can be found here collection:Finders Selection [Keyboard Maestro Wiki]

It’s all worth reading but scroll down to the fifth part

5. Combining Multiple Actions in One For Each Item Action

and you’ll see how to rename a selection of items in the Finder and include a sequence number in the new name.

Hope that helps you and - yes - KM and QK are very different!

Thanks for the quick response @tiffle I did find that one already but the bit i'm struggling with is to get the sequence to reflect the date created.. So prefix 01 is the file that was first created etc etc..

1 Like

@tiffle aha I have found a very easy solution! If I sort the files in finder before running the Macro by date created then this happens automatically for me. Thanks again for your help!

1 Like

An additional very useful feature to my Macro would be if it could check for a number prefix and delete it if it finds one.. I'm messing around with searching and replacing with regular expression, would be great to know if I'm barking up the right tree? :slight_smile:

Many thanks!

Martin

Hey Martin,

Yes a regex search/replace is the way to go.

I don't test first when doing this sort of operation; I just run the regex and let it do its thing. If the prefix is there it will be deleted – if the prefix is NOT there the replace will fail silently.

-Chris

Thanks for that @ccstone, being a total reg ex novice it would be great to be pointed in the right direction to achieve an expression to single out a three digit number and a space at the start of a string?

Hey Martin,

Here you go.

Remove a Number from the Beginning of a String v1.00.kmmacros (6.5 KB)

I recommend BBEdit for learning RegEx and other things because:

  • Although BBEdit is a commercial product, the trial version reverts to a
    still very powerful and free (BBEdit-Lite) version after 30 days.
  • It is very Mac-like.
  • It has excellent support for RegEx.
  • It is very AppleScriptable.
  • It has a two versatile command line tools.
  • It has been in continuous development for 25 years.

I also recommend https://regex101.com

There are other useful tools, but these are two of the best.

-Chris

1 Like

Great. Additionally, if you want to rename all the files in a folder (instead of just a few that you’ve selected) then the Folder Contents collection collection:Folder Contents [Keyboard Maestro Wiki] can be set to sort by create date so you don’t have to!

Anyway - glad it’s working and that @ccstone has given you the solution to your other request!

Thank you both @tiffle and @ccstone ! I have the Macro doing exactly what I want it to do and it's saved me loads of time already!

I have a feeling that i've only scratched the surface of this program and will benefit from taking a deeper dive at some point to see if I can automate some more boring repetitive stuff.

Thanks so much again for the solutions and also for getting back to me so quickly!

1 Like