Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Add numbered sequence to original file names

Is there a way of adding a text in batch with a sequenced number before the original file names?


Let's say I have these files:

stock6920689

stock03874

stock4169463


I want to rename them to:

EP01_001stock6920689

EP01_002stock03874

EP01_003stock4169463


Thank you

Mac Pro

Posted on Nov 25, 2021 4:48 PM

Reply
2 replies

Nov 26, 2021 7:02 AM in response to batmac

What you want to do is unsupported in the Finder's Rename… facility. It can prepend a character string, but not increment it too.


This can be done in a Terminal-based shell script, and with more code, in AppleScript. Are these files in a flat folder, or a folder hierarchy? Do the files intended for this rename have the word "stock" in them, and presumably, they have filename extensions?

Nov 26, 2021 8:16 AM in response to batmac

Here is an example of an AppleScript that recursively explores a folder name for any files containing the keyword "stock" and then proceeds to prepend the string EP01_nnn to the original filename. Tested: macOS 11.6.1.


# prepend_stock.applescript

# Prompt user for folder containing candidate files, and then prepend string EP01_nnn
# to the filenames containing the "stock" keyword in their name.

use scripting additions

property PREFIX : "EP01_"
property KEYWORD : "stock"
property counter : 1 as integer

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

tell application "Finder"
	set flist to (every item of entire contents of folder theFolder whose name contains KEYWORD) as alias list
	if flist = {} then return
	repeat with afile in flist
		set padcnt to text -3 thru -1 of ("000" & counter)
		set pre_name to (PREFIX & padcnt) as text
		set name of afile to pre_name & name of afile
		set counter to counter + (1 as integer)
	end repeat
end tell
set counter to 1
return


Launch the Script Editor, copy/paste the preceding code into it, click the compile (hammer icon), and then run it. It will process all files in the folder containing the word "stock" in them.

Add numbered sequence to original file names

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