Bulk file rename automation

Hello,


likely a simple task for experts using Automator or Apple Script, but I am not an expert.


I have roughly 900 pictures (jpg) that I need to rename. The original file name is a 6-digit number (example: “107453") and I need to rename all files into "107453-107453C" by adding a hyphen after the original name, then repeating the original file name and adding a letter.


If anyone know a way to build that script, it would be super cool.


Thank you !

Posted on Jul 23, 2020 5:56 AM

Reply
Question marked as Top-ranking reply

Posted on Jul 23, 2020 12:50 PM

Well, there was a coding oversight, and this now renames 123456.jpg to 123456-123456C.jpg.


-- prompt for parent folder of images, and then rename each image to the following
-- format: nnnnnn-nnnnnnC.jpg from original nnnnnn.jpg
-- Tested: macOS Mojave 10.14.6 (18G6020) and macOS Catalina 10.15.6 (19G73)
-- Version:1.2 - fixed oversight in tmp extraction
-- vikingOSX, 2020-07-23, Apple Support Communities, No warranties express/implied.

use scripting additions

property images : {"jpg", "JPG", "jpeg", "JPEG"}
property suffix : "C"

set sfolder to (choose folder default location (path to desktop))

tell application "Finder"
	set imglist to (every item in folder sfolder whose name extension is in images) as alias list
	if imglist is {} then return
	
	
	repeat with anImg in imglist
		set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {".", images}}
		set tmp to item 1 of text items of (name of anImg as text)
		set AppleScript's text item delimiters to TID
		set newname to tmp & "-" & tmp & suffix & "." & name extension of anImg
		set name of anImg to newname
	end repeat
	
end tell
return



8 replies
Question marked as Top-ranking reply

Jul 23, 2020 12:50 PM in response to Nicolas M

Well, there was a coding oversight, and this now renames 123456.jpg to 123456-123456C.jpg.


-- prompt for parent folder of images, and then rename each image to the following
-- format: nnnnnn-nnnnnnC.jpg from original nnnnnn.jpg
-- Tested: macOS Mojave 10.14.6 (18G6020) and macOS Catalina 10.15.6 (19G73)
-- Version:1.2 - fixed oversight in tmp extraction
-- vikingOSX, 2020-07-23, Apple Support Communities, No warranties express/implied.

use scripting additions

property images : {"jpg", "JPG", "jpeg", "JPEG"}
property suffix : "C"

set sfolder to (choose folder default location (path to desktop))

tell application "Finder"
	set imglist to (every item in folder sfolder whose name extension is in images) as alias list
	if imglist is {} then return
	
	
	repeat with anImg in imglist
		set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {".", images}}
		set tmp to item 1 of text items of (name of anImg as text)
		set AppleScript's text item delimiters to TID
		set newname to tmp & "-" & tmp & suffix & "." & name extension of anImg
		set name of anImg to newname
	end repeat
	
end tell
return



Jul 23, 2020 8:21 AM in response to Nicolas M

Copy and paste the following into the Script Editor, click the hammer icon (compile), and then the Run button. You can find Script Editor in Dock : Launchpad : Other.


-- prompt for parent folder of images, and then rename each image to the following
-- format: nnnnnn-nnnnnnC.jpg from original nnnnnn.jpg
-- Tested: macOS Mojave 10.14.6 (18G6020) and macOS Catalina 10.15.6 (19G73)
-- vikingOSX, 2020-07-23, Apple Support Communities, No warranties express/implied.

use scripting additions

property images : {"jpg", "JPG", "jpeg", "JPEG"}
property suffix : "C"

set sfolder to (choose folder default location (path to desktop))

tell application "Finder"
	set imglist to (every item in folder sfolder whose name extension is in images) as alias list
	if imglist is {} then return
	
    -- strip dot and file extension from the filename
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {".", images}}
	repeat with anImg in imglist
		set tmp to item 1 of (name of anImg as text)
		set newname to tmp & "-" & tmp & suffix & "." & name extension of anImg
		set name of anImg to newname
	end repeat
	set AppleScript's text item delimiters to TID
end tell
return



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Bulk file rename automation

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.