Rename files sequentially.

Hi everyone. I need your help. I‘m trying to rename multiple files that have a pattern like :

AJTHGH-BK-48367.jpg AJTHGH-BK-48368.jpg AJTHGH-BK-48369.jpg AJTHGH-BK-48370.jpg

that needs to be renamed into :

AJTHGH-BK-1.jpg AJTHGH-BK-2.jpg AJTHGH-BK-3.jpg AJTHGH-BK-4.jpg

so instead of 5 digits i need to use 1,2,3,4,5… but pay attention on the filenames. hope that make sense. I gave 3 examples…

Replies

There’s two parts to this:

  • Working out the new name for the old name?

  • Renaming the file?

I suspect you’re asking about the first one. If so, I presume that you start out with the list of file names that need to be renamed in a batch [1]. Do you know whether that list is already sorted? That is, in your first example, is there any chance that AJTHGH-BK-48367.jpg could be anywhere other than the first item?

And can there be any gaps? That is, could you have a list like AJTHGH-BK-48367.jpg, AJTHGH-BK-48368.jpg, AJTHGH-BK-48370.jpg, and AJTHGH-BK-48371.jpg. And, if so, do you want there to be a matching gap in the new names?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] You need to start out with the full list because to do this incrementally you’d have to know which file has the lowest number.

Thank you for your answer.

The list is not sorted. The files are named during a photoshoot of products.

So say I’m photographing a pair of pants. I would scan the pants label (example : AJTHGH-BK-)and the 5 digits are the camera generated so AJTHGH-BK-45678.jpg, 79, 80 etc…

Next product would have a different/unique name…and different 5 digits…

There could be some gaps. But the new filename should not have gaps….-1, -2, -3, -4, -5…etc

Thank you very much

The list is not sorted.

That makes it harder. Your first step should be to sort the list. That’s depressingly hard to do in AppleScript. If you search the ’net for “applescript sort list”, you’ll find plenty of advice on that.

Once you have a sorted list (oldNames), this code will create a list a matching list of new names (newNames) that you can use to drive the rename:

set oldNames to {"AJTHGH-BK-48367.jpg", "AJTHGH-BK-48368.jpg", "AJTHGH-BK-48370.jpg", "AJTHGH-BK-48371.jpg"}
set text item delimiters to "-"
set newNames to {}
set i to 1
repeat with thisName in oldNames
    set prefixParts to text items 1 through -2 of thisName
    set newParts to prefixParts & {"" & i & ".jpg"}
    set newNames to newNames & (newParts as text)
    set i to i + 1
end repeat

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"