– Common Function Library Project

collectFiles(extensions, destinationPath, sourcePath)

Last updated April 05, 2006


Steven Ross

Version: 2 | Requires: CF6 | Library: FileSysLib

This UDF will scan (recurse) a given path for a given list of files by extension and then copy them to the path you specify.

Return Values:
Returns nothing.


<cfset destination = ExpandPath("./") & "\!collect" />
<cfset source = ExpandPath("./") />

<cfset collectFiles("asp,htm,html", destination, source) />


Name Description Required
extensions List of extensions to copy. Yes
destinationPath Destination directory. Yes
sourcePath Source directory. Yes

Full UDF Source:

 Scans a directory (or path) for files of a specified extension and then copies them to the path you specify.
 v2 by Raymond Camden. I just cleaned up the var statements.
 @param extensions      List of extensions to copy. (Required)
 @param destinationPath      Destination directory. (Required)
 @param sourcePath      Source directory. (Required)
 @return Returns nothing. 
 @author Steven Ross ( 
 @version 2, April 7, 2006 
<cffunction name="collectFiles" access="public" hint="recurses through a directory and collects the file types you want then outputs to another directory" returnType="void">
    <cfargument name="extensions" required="true" type="string" hint="The extensions you want to gather up csv (list) format ex:(asp,cfm,jsp) ">
    <cfargument name="destinationPath" required="true" type="string" hint="absolute path to storage directory">
    <cfargument name="sourcePath" required="true" type="string" hint="absolute path to source directory">
    <cfset var root = arguments.sourcePath/>
    <cfset var i = "">
    <cfset var absPath = "">
    <cfset var relativePath = "">
    <cfset var writeTo = "">
    <cfset var pathAndFile = "">
    <cfif not directoryExists(arguments.sourcePath)>
        <cfthrow message="Source Directory (#arguments.sourcePath#) not found" detail="You didn't pass in a valid source directory, check the path and try again.">
    <cfloop list="#arguments.extensions#" index="i">
        <cfdirectory name="getFiles" directory="#root#" recurse="true" filter="*.#i#">
            <cfloop query="getFiles">
                <cfset absPath = & "/" />
                <cfset relativePath = Replace(absPath, root, "", "all") />
                <cfset writeTo = ARGUMENTS.destinationPath & "/" & relativePath>
                <cfset pathAndFile = & "/" & />
                <cfif not directoryExists(writeTo)>
                    <cfdirectory action="create" directory="#writeTo#">
                    <cffile action="copy" source="#pathAndFile#" destination="#writeTo#">
                    <cffile action="copy" source="#pathAndFile#" destination="#writeTo#">



Latest Additions

Raymond Camden added
November 04, 2017

Leigh added
May 11, 2016

Raymond Camden added
May 10, 2016

Kevin Cotton added
May 05, 2016

Raymond Camden added
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson