Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.
It would seem obvious that the move action for cffile should give you the option to create a unique filename if the destination directory already contains a file with the same name. It would seem. Yet ColdFusion does not.
So here is a function I put together for a project a while back to do this for me.
<cffunction name="moveFile" access="private" output="false" returntype="string"
hint="Moves a file to a final location while preventing name conflicts. Returns the unique filename">
<cfargument name="sourceFilePath" required="true" type="string" hint="Path to the existing file">
<cfargument name="destinationDirectory" required="true" type="string" hint="Path to the destination directory">
<!--- Extract the filename from the source path --->
<cfset var filename = getFileFromPath(arguments.sourceFilePath)>
<cfset var counter = 1>
<cfset var uniqueFileName = "">
<cfset var destinationFilePath = "">
<!--- Create an exclusive lock specific to the target directory --->
<cflock type="exclusive" timeout="30" name="#destinationDirectory#">
<!--- If a file with the same name already exists at the destination --->
<cfif fileExists(destinationDirectory & "/" & filename)>
<!--- Loop up to 100 times to try create a unique filename --->
<cfloop condition="counter LT 100">
<cfset destinationFilePath = destinationDirectory & "/" & counter & "_" & filename>
<cfset uniqueFileName = counter & "_" & filename>
<!--- Filename does not already exist at the destination --->
<cfset uniqueFileName = filename>
<cfset destinationFilePath = folder & "/" & filename>
<!--- Provided the filename is not already in use at the destination or a unique filename was generated we can perform the move --->
<cffile action="move" source="#sourceFilePath#" destination="#destinationFilePath#">
<cfthrow message="Unable to move the file and create a unique filename at the destination">
Note: While not strictly necessary the cflock is an added guarantee that this code does not encounter a race condition when moving files into a specific directory.