Utility Function: DirectoryCopy()
- November 26, 2008 5:29 AM
- Utility Function
- Comments (0)
I was kind of surprized when I found that ColdFusion has no built in ability to recursively copy the contents of a whole directory. In order to do that, I whipped up this function.
<cffunction name="DirectoryCopy" access="public" output="false" returntype="void">
<cfargument name="source" type="string" required="true" />
<cfargument name="destination" type="string" required="true" />
<cfset var local = StructNew() />
<!--- Get the contents of this directory --->
<cfdirectory action="list" directory="#arguments.source#" name="local.contents" />
<!--- If the destination doesn't exist, create it --->
<cfif NOT DirectoryExists(arguments.destination)>
<cfdirectory action="create" directory="#arguments.destination#" />
</cfif>
<!--- loop over everything in the directory --->
<cfloop query="local.contents">
<!--- Figure out source and desitnation --->
<cfset local.destination = "#arguments.destination#\#local.contents.name#" />
<cfset local.source = "#arguments.source#\#local.contents.name#" />
<!--- If this is a folder, call this on that folder --->
<cfif local.contents.type eq "dir">
<cfset DirectoryCopy(
source = local.source,
destination = local.destination
) />
<!--- If this is a file, copy it over --->
<cfelse>
<cffile action="copy" source="#local.source#" destination="#local.destination#" />
</cfif>
</cfloop>
<cfreturn />
</cffunction>