Renumbering a Collection of Folders - Removing Existing Numbering but Keeping Order the Same?

I have a macro that I run to create a series of folders up at the start of a project that contains sub-folders. Sometimes, mid way through the project, I realise that I needed some more entries and so to keep things where I need them, roughly, I copy a folder and amend the name. Because of that, I end up with a folder structure like this (with sub folders containing files):

1 - Header
1 - Header - review and Submit Panel
2 - Footer
3 - Dashboard
4 - Course Sidebar - Completed Courses
4 - Course Sidebar - Open Courses
5 - Course Sidebar - Do you need to add materials to this adoption?
5 - Course Sidebar - Don't See Your Course?

What I want to do is run a macro at the end which re-numbers (removes the existing number, keeping everything in same order), so I would end up with something like this:

1 - Header
2 - Header - review and Submit Panel
3 - Footer
4 - Dashboard
5 - Course Sidebar - Completed Courses
6 - Course Sidebar - Open Courses
7 - Course Sidebar - Do you need to add materials to this adoption?
8 - Course Sidebar - Don't See Your Course?

And it probably goes without saying, I want the contents of each folder to remain intact.

I presume that this would require a combination of regex and some shell scripting of some kind, neither of which are strong points of mine.

Does anyone have any scripts that might do what I need here? Or is there a simpler way to do this than regex/shell?

Thanks all

Yes, this can be done.

Can the numbers ever exceed 9? Because ordering is often messed up after 9, with 10 coming alphabetically between 1 and 2 unless you have a number aware sort. The Finder and Keyboard Maestro’s sort will both generally do this correctly, but other things may not.

The other issue that can catch things out is that if you rename a folder to a new name, that new name might already be occupied by a later folder that has not been renamed yet. For example, if you had a folder containing:

  • 1 - Alpha
  • 1 - Header
  • 2 - Header

Renaming 1 Header to 2 Header would be a problem if it was done before 2 Header was renamed to 3 Header.

If there are never any missing numbers (like there might be three that start with 1 but there will never be one that starts with 3 if there are not also ones that start with 1 and 2), then you can rename the files in reverse order and that would always be safe. Otherwise you could have a case like this:

  • 1 Footer
  • 3 Footer
  • 4 Footer

And the rename from 4 Footer to 3 Footer will fail.

Don't you love computes? Something so simple because so complex when you have to specify everything exactly.

The easiest solution to this is to rename the files in two passes, and include a unique addition in the first rename so every file is guaranteed to be unique in each rename.

For example, in the first example, rename 1 - Header to KMTEMP - 2 - Header on the first pass, and then rename it to 2 Header on the second pass.

Here is a macro that does this. The desired folder should be open in the Finder.

DO NOT RUN THIS UNTIL YOU HAVE A FULL COMPLETE BACKUP

Basically, never run any file system manipulation automation unless you have a full and complete and up to date backup system. It is way to easy for things to go wrong.

The macro makes three passes. The first verifies that every single file in the folder starts with DIGITS, then SPACE, then DASH, then SPACE.

The second renames and indexes the files, with a unique addition so every file will have a new name and will never match any original name. The third pass removes the unique tag.

Rename Numbered Files.kmmacros (7.5 KB)

1 Like

Simpler? I'd say that entirely depends upon what you know and what you don't...

I reiterate Peter's admonition...

For fun I thought I'd take a crack at this in the shell.

It's not sophisticated and has no error-checking other than zsh's built-ins, but it works – and I got to practice file manipulation in the shell.


Download: Rename Numbered Items in Front Finder Window Sequentially (Shell) v1.01.kmmacros (6.7 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

1 Like

I got a twofer! I'll give this a try and, of course, only on test files until I am sure that it works. Peter's solution worked for me, but would a shell operation be more performant, do you think?

Thanks folks - I really appreciate that you are able to not just advise on how but actually provide solutions!

1 Like

Don't use the 1.00 version – it had an issue with the sort.

I've posted v1.01 with a fix.

Make that a threefer... :sunglasses:

I suspect the shell version will be a trifle faster than Keyboard Maestro.

I thought I'd have a go at doing this with AppleScript as well, since a pure rename operation using the Finder is not very risky.

The headache with vanilla AppleScript was doing a proper sort, but by using AppleScriptObjC we now have access to more sophisticated methods and sorting is fairly straightforward.


Download: Rename Numbered Items in Front Finder Window Sequentially (AppleScript) v1.00.kmmacros (7.6 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

How often are you doing this, and over how many files?

Unless the answer is often and thousands, then performant is irrelevant, and what is relevant is which one do you (and future you!) find easier to understand and maintain (which depends on your familiarity with AppleScript, Shell Script, and Keyboard Maestro).

3 Likes