coldfusioncffile

Can I use cffile to copy multiple files?


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?


Solution

  • 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">