Renaming a file to a file that already exists

I have a short script that overwrites an existing file to a name in the clipboard. The problem is that this doesn’t work if a file with that name already exists in the folder. This is the short code I have composed:

set the_file to (("MACVOL1:MT:ibase:Exporter:") & "export1.sgm") as file specification
tell application "Finder"
set name of the_file to the clipboard
end tell

If you have a base dir and a file name, you can try this (please, note that you don’t need the Finder to access the clipboard!):

set sampleBaseDir to "path:to:folder:"
set sampleFileName to (the clipboard)
set increaseNumber to 0
set finalFile to sampleBaseDir & sampleFileName

repeat
	try
		finalFile as alias --> will throw error if file already does not exist
		--> no error = file exists = try new name
		set increaseNumber to increaseNumber + 1
		set finalFile to sampleBaseDir & sampleFileName & "." & increaseNumber
	on error --> file does not exist!
		exit repeat
	end try
end repeat
return finalFile

In this sample, we have in the clipboard the text “whatever.txt”.
In the sample folder “path:to:folder:” we have two files called “whatever.txt” and “whatever.txt.1”.
So, the result for this code will be:

--> "path:to:folder:whatever.txt.2"

You can make it more complex and reliable, by adding the increase-number before the extension, checking whether the resultant file-name is valid (eg, the name is less than 250 characters)

Thanks for the code. Unfortunately it quite didn’t do what I wanted. I wanted to overwrite the current file with the same name, not a new name. But after modifying some code I’ve found here I came up with this one.

set filName to (("MACVOL1:MT:X:Exporter:") & (the clipboard))
set the_file to (("MACVOL1:MT:X:Exporter:") & "export.sgm") as file specification
tell application "Finder"
  if exists file filName then
     delete file filName -- moves it to the trash
  end if
  set name of the_file to the clipboard
end tell

Dont have much experience in Applescript since I’m originally a PC programmer but find it to be an excellent language, but I have a lot to learn.