Easiest way to rename x number of files from a text file with x number of lines?

I can think of a few ways that I could do this, but they are all a bit ... clunky.

Scenario is this (numbers for demo purposes only):

  • 100 files in sequential order (some kind of file count)
  • A text file with 100 lines in
  • Need to rename each file with the respective line in the text file

What is the cleanest, most performant way to do this?

Fun! Will the number of files and lines in text file always match?

Personally because of my lack of experience I might look at writing a Python script that KM would call to do the renaming. I bet it can be done in KM as well but haven’t done that before.

This may help:

It will always match :slight_smile: It doesn't usually work out like that.

Have you got small example sets of the original and final names?

1 Like

To be honest, I didn't provide sample filenames because it really is a case of it being:

recording 1.flac
recording 2.flac
recording 3.flac
recording 4.flac

And a text file:

Artist name - Title 1
Artist name - Title 2
Different Artist name - Title 1
Different Artist name - Title 2

No regex/pattern matching to do, simply a case of replacing the existing filename with the relevant line in the text file, e.g.

recording 1.flac -> Artist name - Title 1.flac

1 Like

So how much "control" do have over the inputs? Will the files be in a single directory -- and will there be other files/directories that shouldn't be renamed? Can you be sure that there will be the same number of lines in the text file as there will be files to rename? If everything's in the same directory, can you be sure that every line in the text file is unique?

File renaming isn't a destructive process, but it can be an absolute PITA to revert if it goes wrong part way through... Can you stand to duplicate the source directory, rename there, and delete the original after an eyeball? Or do we need to work in place and include things like uniq-ing the text file to check for duplicates, counts to make sure numbers match, and similar?

Coz the simplest method is to maintain a counter i and "For Each File in a Directory" rename to line i of text file then increment i. No safety net in that method though!

Total control of output, and it's simple flat file with an incrementing counter. File count and text count will absolutely match 1:1. Also, every line in text file will be unique (similar, but unique). For this particular process, I don't need a safety need for this process; they exist elsewhere in the workflow (if it went wrong, I have another version to go back to very quickly).

Given that, and assuming your files' alphabetical sort matches your name list order:

Rename From File.kmmacros (4.2 KB)

Image

Nothing clever in there, and on my 2015 iMac with spinning rust storage it'll rename 1,000 files in a tad under 4 seconds.

4 Likes

Apologies, I completely forgot to come back to this. I just needed to do this and thought "I'm sure I asked this question before" and sure enough, past-me did just that. Works perfectly :slight_smile: