File renaming problem: 1, 3, 7 to 1, 2, 3

Hello,

I’m trying to rename files in a given folder to consecutive numbers: 1.jpg, 2.jpg, 3.jpg etc. This works okay, if the filenames are not already numbers (e.g. a.jpg or b3.jpg etc.). But if I have a folder with these three files:

1.jpg
3.jpg
4.jpg
7.jpg
8.jpg
12.jpg

where I have deleted the other files and want to rename the remaining files to 1, 2, 3 etc. without changing their order (with 3.jpg becoming 2.jpg and 7.jpg becoming 3.jpg etc.), I get an error, telling me that “an object with this name alreay exists”. The problem, I would guess, is when the script tries to rename 1.jpg to 1.jpg. So:

How can I either force the renaming to the same name, or skip the renaming, if the name already is as it should be?

Here is the relevant part of my script:


-- script opens folder, sets ix to 0 and then tells Finder to:
set allItems to (get every item of theFolder)
repeat with thisItem in allItems
	if name extension of thisItem is "jpg" then
		set ix to ix + 1
		set newName to (ix as string) & ".jpg"
		set name of thisItem to newName
	end if
end repeat
-- rest of script ...

If I add try, like this:


		try
			set name of thisItem to newName
		end try

I get no error and some files are renamed, but some are not.

Any ideas?

AppleScript: 1.10.7
Browser: Firefox 2.0.0.1
Operating System: Mac OS X (10.4)

Hi,

if you implicitly want to keep the order of the files,
then you can rename them twice, first with a distinctive prefix like “temp_”
and the second time to the numbers you want.

Here is a simple solution in case the order is not so important.
If a filename already exists, an error occurs and the counter
will be incremented, otherwise the repeat loop will be left immediately

tell application "Finder"
	set ix to 0
	set allItems to (get every item of theFolder whose name extension is "jpg")
	repeat with thisItem in allItems
		repeat
			set ix to ix + 1
			try
				set name of thisItem to (ix as string) & ".jpg"
				exit repeat
			end try
		end repeat
	end repeat
end tell

Hi Manfred,

The problem is probably in your naming convention. It’s easier if you use leading zeros (i.e. 01, 02, … or 001, 002, … depending on how many files you plan to have). When you get the list of files:

1.jpg
3.jpg
4.jpg
7.jpg
8.jpg
12.jpg

it won’t be in that order. “12.jpg” will come before “3.jpg”, so your renamed files will be out of order. Furthermore, if you had “13.jpg”, you would rename that file to “3.jpg” and there will be an error because the file already exists. You can rename a file with the same name and no error will occur also, so this is probably why you’re getting errors.

Try adding leading zeros and the Finder will auto sort your lists.

gl,

Hi kel,

I don’t really want to use leading zeros, because then I would have to update several php scripts and a database that handle the filenames. Can I tell the script to order the files as numbers instead of as strings, like I can in php?

Hello Stefan,

renaming the files twice is an option, but a danger also, since I really don’t know what names the files might have (and the idea of automation is that I don’t have to check), because the folders with files come from different people and I don’t know how they have named their files. Of course, the possibility that the names are the same is rather small, but over time I’m renaming thousands of files and I don’t want to have even one dead link on my website.

I’ll play around a bit more, and maybe someone here has another idea, otherwise I’ll do the double renamer.

Thank you, so far.

PS: An other way is to move the files
consecutively to a new folder and rename them there

Stefan, I have tested your suggestion to rename the files twice, first to tmp_ + number and then to number. The problem, of course, is what kel mentioned: after the first renaming the files are ordered tmp_1, tmp_10, tmp_12, tmp_2, tmp_3, … , and when I rename them a second time, what was once the 10th file is now 2.jpg.

So, instead of a double renaming routine, I test for the file name like this (part of script only):


set ix to 0
set allItems to (get every item of theFolder)
repeat with thisItem in allItems
	if name extension of thisItem is "jpg" then
		set ix to ix + 1
		set newName to (ix as string) & ".jpg"
		if name of thisItem is not newName -- test for filename
			set name of thisItem to newName
		end if
	else
		-- delete folders and non-image files
		delete thisItem
	end if
end repeat

Since I can tell PHP to get the files in a numeric order (1, 2, 10, not 1, 10, 2), it does not matter to me that now the files are “unordered”, from the string point of view.

Now I have to make the script make Photoshop create thumbnails. Wish me luck. I’ll be back with questions soon :slight_smile:

Ha ha, well, I’m already back, since I made a mistake.

Everything works as I said, if the files at the beginning are numbered with leading zeros like this:

001.jpg → 1.jpg
003.jpg → 2.jpg
012.jpg → 3.jpg

If I begin with files without leading zeros like this:

1.jpg → 1.jpg
3.jpg → 3.jpg
12.jpg → 2.jpg

I end up with 3 and 12 switched in the end.

So how can I get my script to process these files in a numeric order (like in PHP with sort($array, SORT_NUMERIC)) or insert leading zeros before I re-number the files? Can I do something like “if filename consists of 5 characters (i.e. number + dot + extension) then insert 00 else if …”?

If your file names are as basic as your example ones you may be able to pad zeros like this. The first line is where to set the number lenght.

set the ZeroPadder to 5
set inputFolder to choose folder with prompt "Where is the folder of files to re-number?"
--
tell application "Finder"
	set filesList to (files of entire contents of inputFolder)
	repeat with aFile in filesList
		set theFile to aFile as alias
		set thisName to name of theFile
		--
		set ASTID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "."
		set thisNumber to text item 1 of thisName
		set AppleScript's text item delimiters to ASTID
		--
		set theZeros to ""
		repeat until (length of (theZeros as text)) = (ZeroPadder)
			if theZeros = "" then
				set theZeros to thisNumber
			else
				set theZeros to "0" & theZeros
			end if
		end repeat
		set name of file theFile to (theZeros as string) & ".jpg"
	end repeat
end tell