Renaming files problem

I’m trying to rename a load of files in a folder which is working fine, but when it comes accross a particular file name it tries to rename the file twice instead of moving onto the next file.

	tell application "Finder" to set the source_folder to folder "Macintosh HD:Users:nik:Desktop:rename folder"
end try
repeat with i from 1 to number of items in source_folder
	set this_item to item i of source_folder
	set theextention to ".pdf"
	set thefilename to the name of this_item
	set thepagenemuber to characters 17 thru 19 of thefilename as string
	set parta to characters 1 thru 4 of thefilename
	set partb to characters 6 thru 8 of thefilename
	set partc to characters 10 thru 11 of thefilename
	set partd to characters 13 thru 14 of thefilename
	set the name of this_item to thepagenemuber & "_" & parta & "_" & partb & "_" & partc & "_" & partd & theextention
end repeat

The file thats being processed twice has this file name: mrwk-sep-01-06-pobc.pdf it first renames it to the correct name: obc_mrwk_sep_01_06.pdf but instead of skipping onto the next file it tries to reprocess this file again giving the result: 06._obc__rwk_se__0.pdf. This seems to be a problem with the letter “o” as any other combination of letters works.
Please help!!!

It’s not a bug blend3; After you rename the item, the order of the items change; When you loop back around and ask for.

set this_item to item i of source_folder

. you don’t get what you expect.

Also, you should use text instead of characters; Text returns text, but characters returns a list, which you later coerce back into text (where text item delimiters could mess you up).

Try something like this:

tell application "Finder"
	choose folder with prompt "Rename files in this folder:"
		set fileList to files of (result) whose name extension is "pdf"
		repeat with thisItem in fileList
			set thisName to name of thisItem
			set name of thisItem to ¬
				text 1 thru 4 of thisName & "_" & ¬
				text 6 thru 8 of thisName & "_" & ¬
				text 10 thru 11 of thisName & "_" & ¬
				text 13 thru 14 of thisName & ".pdf"
		end repeat
	on error errMsg number errNum
		display dialog "Error " & errNum & ":" & return & return & errMsg buttons {"Cancel"} default button 1 with icon caution
	end try
end tell

That works great.
Thanks for the logic lesson
Nik :slight_smile: