Trouble with renaming string, due to duplicates

I am currently trying to finish off a script that will change the file names of some pdf’s, the script needs to run automaitcally and I need it to run through any errors it finds.

The part I am stuck on is as follows

repeat with eachfile in folder (alias “location”)
set x to the name of eachfile as text
set y to x’s text 1 thru 6
set name of eachfile to y & “.pdf”

What I need to add is an error handler that will change the name of the file if it encounters a file named the same in the directory it is working on. e.g. add “-1” to the file name if its a duplicate.

I have looked to put a time stamp on the end of each file its renaming to ensure it is unique and would be equally useful, but sadly my skills are still developing.

Can anyone offer any advice or help.

insomniac22,

Not sure if this is what you’re looking for but here 'tis.

set newName to ""
tell application "Finder"
	set theChoice to choose folder with prompt "Choose folder with files for renaming."
	set fileList to every file of folder theChoice
	set theDate to current date
	if month of the (current date) is January then
		set mnthNum to 1
	else if month of the (current date) is February then
		set mnthNum to 2
	else if month of the (current date) is March then
		set mnthNum to 3
	else if month of the (current date) is April then
		set mnthNum to 4
	else if month of the (current date) is May then
		set mnthNum to 5
	else if month of the (current date) is June then
		set mnthNum to 6
	else if month of the (current date) is July then
		set mnthNum to 7
	else if month of the (current date) is August then
		set mnthNum to 8
	else if month of the (current date) is September then
		set mnthNum to 9
	else if month of the (current date) is October then
		set mnthNum to 10
	else if month of the (current date) is November then
		set mnthNum to 11
	else if month of the (current date) is December then
		set mnthNum to 12
	end if
	
	set timeStamp to mnthNum & "_" & day of theDate & "_" & year of theDate as text
	repeat with i from 1 to (count fileList)
		set nameList to name of every file of folder theChoice as list
		set fileName to name of item i of fileList
		if (length of fileName) is not less than 6 then
			set newName to text 1 thru 6 of fileName
		else
			set newName to fileName
		end if
		display dialog item 1 of nameList as string
		if newName is not in nameList then
			set properties of item i of fileList to {name:newName}
		else
			set otherName to newName & timeStamp
			set nameList to name of every file of folder theChoice as list
			if otherName is not in nameList then
				set properties of item i of fileList to {name:newName & timeStamp}
			end if
		end if
	end repeat
end tell

There might be an easier way to get the months to numbers but I don’t know what it is.

Hope this helps.

PreTech

Well now how simple is that!:smiley:

To guarantee a unique ID, you can use the shell command “uuidgen”:

set theUniqueID to do shell script “uuidgen”
set the newName to (text 1 thru 6 of oldName) & theUniqueID

This way there is no chance that you’ll make two files with the same name.

Thanks guys, you have all put me on the right track now.
Should not have too much trouble using a unique ref.

cheers

m.

Thanks MTV for pimpin my ride!