Utility Function: DirectoryCopy()

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#" />
    <!--- 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 --->
            <cffile action="copy" source="#local.source#" destination="#local.destination#" />
    <cfreturn />



Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me.

Post a job. Find one. authenticjobs.com

Interested in becoming a sponsor? Contact me.