2

In ColdFusion I am setting up a new directory with <cfdirectory. Then I need to copy several files from one directory to the new one, keeping the same filenames. I can use a loop to do this, but am wondering if there is any function in cffile that would allow me to copy multiple files at one time.

Cumbersome -- With reploc and newloc being source and target directories:

<cfdirectory
      directory  = 'newloc'
      action     = 'create'   
      mode       = 777>

<cfoutput>
<cfset extrep = ExpandPath('reploc')>
<cfset extnew = ExpandPath('newloc')>

 <cfset flist = 'a.cfm', 'b.cfm'>
 <cfloop list = '#flist#' index = 'item'>
   <cffile 
       action = "copy" 
       source = "#extrep#/#item#"
       destination = "#extnew#/#item#"
       mode = "766" >
 </cfloop>
 </cfoutput>

I have exactly four files to copy. They are fixed and do not depend on any user input.

I was hoping <cffile would support copying multiple files, but I can't find anything that says it will. Can anyone suggest a more streamlined approach to setting up this directory with its four files?

Betty Mock
  • 1,373
  • 11
  • 23

1 Answers1

8

Nope. As the name implies, cffile only operates on individual files.

However, what you could do is use DirectoryCopy() or <cfdirectory action="copy"> with a file filter. The example below copies files "a.cfm" and "b.cfm" to the target folder.

CFScript/CFML:

 DirectoryCopy("c:\path\source", "c:\path\target", false, "a.cfm|b.cfm")

CFML:

<cfdirectory action="copy"
    directory="c:\path\source"
    destination="c:\path\target"
    filter="a.cfm|b.cfm">
SOS
  • 6,430
  • 2
  • 11
  • 29
  • Will try this shortly. – Betty Mock Jul 17 '19 at 18:58
  • Glad it helped. Turns out there actually IS a DirectoryCopy function, it was just omitted from one of the docs. It does the same thing and IMO is sleeker than using ``. See updated answer. – SOS Jul 17 '19 at 20:33