CFLib.org – Common Function Library Project

createUniqueFileName(fullpath)

Last updated July 1, 2008

Version: 3 | Requires: ColdFusion 5 | Library: FileSysLib

 
Rated 0 time(s). Average Rating: 0

Description:
Pass a full file path to this function; if the file exists, the function will return a new, unique file name.

Return Values:
Returns a string.

Example:

view plain print about
<cfset newFile = CreateUniqueFileName(GetCurrentTemplatePath())>
<cfoutput>Your New File Name: #newFile#</cfoutput>

Parameters:

Name Description Required
fullpath Full path to file. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Creates a unique file name; used to prevent overwriting when moving or copying files from one location to another.
 * v2, bug found with dots in path, bug found by joseph
 * v3 - missing dot in extension, bug found by cheesecow
 * 
 * @param fullpath      Full path to file. (Required)
 * @return Returns a string. 
 * @author Marc Esher (marc.esher@cablespeed.com) 
 * @version 3, July 1, 2008 
 */

function createUniqueFileName(fullPath){
    var extension = "";
    var thePath = "";
    var newPath = arguments.fullPath;
    var counter = 0;
    
    if(listLen(arguments.fullPath,"."gte 2) extension = listLast(arguments.fullPath,".");
    thePath = listDeleteAt(arguments.fullPath,listLen(arguments.fullPath,"."),".");

    while(fileExists(newPath)){
        counter = counter+1;        
        newPath = thePath & "_" & counter & "." & extension;            
    }
    return newPath;    
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
4 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson