CFLib.org – Common Function Library Project

collectFiles(extensions, destinationPath, sourcePath)

Last updated April 5, 2006

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:

view plain print about
<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:

view plain print about
<!---
 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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
4 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson