CFLib.org – Common Function Library Project

collectFiles(extensions, destinationPath, sourcePath)

Last updated April 05, 2006

author

Steven Ross

Version: 2 | Requires: CF6 | Library: FileSysLib

Description:
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.

Example:

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

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

Parameters:

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 (steven.ross@zerium.com) 
 @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.">
    </cfif>
    
    <cfloop list="#arguments.extensions#" index="i">
        
        <cfdirectory name="getFiles" directory="#root#" recurse="true" filter="*.#i#">
    
            <cfloop query="getFiles">
                
                <cfset absPath = getFiles.directory & "/" />
                
                <cfset relativePath = Replace(absPath, root, "", "all") />
                
                <cfset writeTo = ARGUMENTS.destinationPath & "/" & relativePath>
                
                <cfset pathAndFile = getFiles.directory & "/" & getFiles.name />
                
                <cfif not directoryExists(writeTo)>
                    <cfdirectory action="create" directory="#writeTo#">
                    <cffile action="copy" source="#pathAndFile#" destination="#writeTo#">
                <cfelse>
                    <cffile action="copy" source="#pathAndFile#" destination="#writeTo#">
                </cfif>
                
            </cfloop>
            
    </cfloop>

</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson