Changing the name of a file... you'd think it would be easy.

Ok i am writing a script that includes a portion where a file needs to be copied from one folder to the next. To add some intelligence to the process I want to rename the source file if a file of the same name already exists at the destination.

The process seems quite simple and its flow should be: set source folder; set destination folder; get the name of the first file in the source folder; see if the same name exists in the destination folder; if it does rename it by adding “001” to the filename; if it doesn’t exist then leave it alone.

The bit I can’t get to work is the renaming of the file! This I thought would be the easy bit!!!

I’ve trawled various message boards looking for a solution, have tried a number of methods but to no avail. I am sure my paths are correct because if I change the line that tries to rename the file to one that deletes the file and that works.

If anyone can tell me where I am going wrong I’d be very grateful. I’m running OS X 10.4.11

The script I’m using is this:

-- set path of the source folder

set hot_folder to "Macintosh HD:Users:User:Desktop:HotFolder:"

-- set path of the destination folder

set dest_folder to "Macintosh HD:Users:User:Desktop:DestinationFolder:"

--picks up the name of the second file in the source folder

tell application "System Events"
	set file_name to name of file 2 of folder hot_folder
end tell

--set the full path of the file to be dealt with original path + name

set hot_file to hot_folder & file_name

--check to see if a file of the same name exists at the destination reports condition and renames original file if needed.

set dest_file_check to dest_folder & file_name
tell application "Finder"
	if exists file dest_file_check then
		display dialog dest_file_check & " already exists"
		set AppleScript's text item delimiters to "."
		set filenamePart1 to text item 1 of file_name
		set filenamePart2 to text item 2 of file_name
		set hot_file_new_name to hot_folder & filenamePart1 & "001." & filenamePart2 as string
		set name of hot_file to hot_file_new_name
		display dialog dest_file_check & " does not exist"
	end if
end tell

When I run this script and it tries to rename I get an Error: Can’t set name of “blah blah…” to “blah blah001”


try this, it copies all files from HotFolder to DestinationFolder considering the renaming.
It uses a shell command, because with /bin/cp you can copy and rename at the same time

-- set path to desktop
set dt to path to desktop as text

-- set path of the source folder
set hot_folder to dt & "HotFolder:"

-- set path of the destination folder
set dest_folder to dt & "DestinationFolder:"

-- get all files of HotFolder
tell application "Finder" to set theFiles to files of folder hot_folder
repeat with oneFile in theFiles
	set Nm to name of (info for oneFile as alias)
	set sourceFile to quoted form of POSIX path of (oneFile as alias)
	set newName to checkFileName(dest_folder, Nm, "")
	set destinationFile to quoted form of POSIX path of newName
	do shell script "/bin/cp " & sourceFile & space & destinationFile
end repeat

on checkFileName(fDir, fName, separator) -- fDir can be alias or path string
	set fDir to fDir as Unicode text
		set f to (fDir & fName) as alias
		set {name:Nm, name extension:Ex} to info for f
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set idx to 0
			set idx to idx + 1
			set checkName to (fDir & Nm & separator & (idx as text) & "." & Ex)
				checkName as alias
			on error
				return checkName
			end try
		end repeat
	on error
		return (fDir & fName)
	end try
end checkFileName

If you want a separator between the name and the index, specify the character in this line

set newName to checkFileName(dest_folder, Nm, "-")

in this example the new name is xyz-1.ext. I hope you don’t mind, that the new name has not three digits like xyz-001.ext

Two things to change

set hot_file_new_name to filenamePart1 & "001." & filenamePart2 as string
set name of file hot_file to hot_file_new_name

Hi James,

Thanks for that I wrestled with subtle changes to syntax but never got to the version you came up with.

Thanks also to Stephan who came up with some useful suggestions that’ll be useful in the future.

I’ve opted to use James’ suggestion as, in the script as a whole, I need to extract a lot more from the source file before moving it so a rename and move at the same time doesn’t work for me in this instance.