CFLib.org – Common Function Library Project

collectFiles(extensions, destinationPath, sourcePath)

Last updated April 5, 2006
Download UDF

author

Steven Ross                                       Steven Ross

Version: 2 | Requires: ColdFusion MX | Library: FileSysLib

 
Rated 0 time(s). Average Rating: 0

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

Alan McCollough Alan McCollough added
forceBoolean
1 day(s) ago

Shawn Porter Shawn Porter added
DeMoronize
4 day(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
5 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson