Renaming a series of files on a Mac

I need some Mac help here, solving a situation I find myself in fairly often.

If I have a folder full of files --let’s say they were jpegs–with more-or-less random file names, how can I rename them in chronological order?

I’d want the first file created (“alkcbfkj.jpg”) to be renamed “file1.jpg” and the second file created to be “file2.jpg”, etc.

I’m slightly familiar with the file renaming scripts (Replace text in file names, Append text to file names) available in the Finder Scripts pulldown, but don’t see anything which would let me rename a bunch of files in chronological order.

http://creativebe.com/renamer4mac/
This should work. When you rename the contents of the folder make sure the folder is sorted by date.

Use Automator.

In days of yore, I used FileBuddy to perform that function, and more. The info says that it will “rename files based on patterns, sequences and dates”, which sounds like what you want.

Someone who remembers more about awk will be along in a moment to show you a one-line command from Terminal that will do the job, but here’s a quick applescript:




on run
	set myfilenameprefix to "File"
	set thefolder to choose folder
	tell application "Finder"
		set allthenames to name of every file in folder thefolder
		set fcount to count allthenames
		if fcount > 0 then
			repeat with i from 1 to fcount
				set curname to item i of allthenames as text
				set newname to my renamefile(curname, i)
				try
					set name of file curname in folder thefolder to newname
				on error
					beep
				end try
			end repeat
		end if
	end tell
	say "the mighty groo gave me permission to rename my files"
end run

on renamefile(aname, anumber)
	global myfilenameprefix
	set theout to my getextension(aname)
	set thename to item 1 of theout
	set theextension to item 2 of theout
	set newname to myfilenameprefix & my leadingzeroes(anumber) & "." & theextension
	return newname
end renamefile
on leadingzeroes(anint)
	if anint > 100 then
		set anout to "-" & anint as text
	else if anint > 10 then
		set anout to "-0" & anint as text
	else
		set anout to "-00" & anint as text
	end if
	return anout
end leadingzeroes

on getextension(afilename)
	set AppleScript's text item delimiters to "."
	set text_item_list to every text item of afilename
	set AppleScript's text item delimiters to ""
	set icount to count text_item_list
	if icount = 0 then
		beep
		set theoutput to {afilename, ""}
	else
		if icount = 1 then
			set theoutput to {afilename, ""}
		else if icount = 2 then
			set theoutput to {item 1 of text_item_list, item 2 of text_item_list}
		else
			set theoutname to item 1 of text_item_list
			repeat with i from 2 to icount - 1
				set theoutname to theoutname & "." & (item i of text_item_list)
			end repeat
			set theoutput to {theoutname, item icount of text_item_list}
		end if
	end if
	return theoutput
end getextension


Note: I had getextension just lying around; this was about three minutes’ effort.

Open up Applications/Applescript/Script Editor, paste the text above into a new window, then just hit the “play” button on the Applescript editor tool bar. It’ll prompt for a folder name; select your folder and hit “OK.” Note: Change the string “File” to whatever you want as your “base” filename.

I think I’ll finally get around to reading up on Automator; ISTR it was designed precisely for functions like this.

This is a three-step Automator action:

  1. Ask for Finder Items (files, allow multiple selections).
  2. Sort Finder Items (Creation Date, Ascending).
  3. Rename Finder Items (Make Sequential, then whatever you want to name the file).

Save as an application, and then run it. The only issue is - it won’t take a folder as input. You need to select all the files in the folder.

Thanks, gents. I don’t know a thing about Automator, but may give it a whack.

For the one liner, you don’t need awk. You could open up a shell, navigate to the directory the files are in and try this:

[user] i=1; for j in (ls *jpg); do k=$(printf %03d i); i=(($i+1));mv $j file$k.jpg; done

If you like doing that sort of thing.

Automator is easily the most powerful and useful piece of “unknown” software that Apple has released. It’s a brilliant and easy to use tool, and comes with every machine.

I finally got around to messing with this, and managed to assemble an Automator script that works pretty well! Thank you, beowulff!