4

Any idea why CFFILE Write does not support making it unique but CFFile Upload does?

I'm using CFFILE Write to handle a GetHttpRequestData, and being able to support making it unique would be very helpful. Any ideas?

WozPoz
  • 992
  • 2
  • 15
  • 29

3 Answers3

9

Well, it doesn't. You'll have to roll your own unique names by first checking if the file exists and if it does, then add on some additional character(s) and repeat check/add until you've found something unique.

Alternatively, you always prepend something that ought to be fairly unique (e.g. date-time-incremental number).

Gert Grenander
  • 16,866
  • 6
  • 40
  • 43
  • 3
    Gert G is correct. You have to roll your own. But IMO it is always a good idea to check cflib.org first. With most tasks, you will find "someone has already written a function for that ...". At the very least, one you can adapt to suit your needs. cflib.org/udf/createUniqueFileName – Leigh Jul 26 '10 at 01:43
5

try something like this

<cfscript>
    i = 1;
    myPath = 'D:\webroot\sap\returns\log';
    myFileName = orderNumber;
</cfscript>
<cfloop condition="fileExists('#myPath#\#myFileName#.xml')">
    <cfscript>
        myFileName = '#orderNumber#_#i#';
        i += 1;
    </cfscript>
    <cfif i GT 100><cfbreak /></cfif>
</cfloop>

<cffile action="write" file="#myPath#\#myFileName#.xml" output="#xmlString#"  />
Rob D.
  • 51
  • 1
  • 1
  • Yep, that is basically the approach used by [createUniqueFileName](http://http://cflib.org/udf/createUniqueFileName). 'Gotta' love cflib.org. – Leigh Oct 17 '12 at 21:19
4

How about using CreateUUID() as filename?

PPShein
  • 13,309
  • 42
  • 142
  • 227