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#" />
    </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>

 

Utility Function: IfElse()

I don't know about you, but I hate having to write out simple if-else code bits in CF. Until they come out with a good operator like JS has (something?something:something), CF needs a simplification.

<cffunction name="IfElse" access="public" output="false" returntype="Any">
    <cfargument name="test" type="boolean" required="true" />
    <cfargument name="whenTrue" type="any" required="true" />
    <cfargument name="whenFalse" type="any" required="true" />
    
    <cfif ARGUMENTS.test>
        <cfreturn ARGUMENTS.whenTrue />
    <cfelse>
        <cfreturn ARGUMENTS.whenFalse />
    </cfif>    
</cffunction>

Seems pointless, but which would you rather read?

<!--- Classic, 5 lines --->
<cfif true>
    <cfset x = "dog" />
<cfelse>
    <cfset x = "cat" />
</cfif>

<!--- Version 2, 4 lines --->
<cfset x = "cat" />
<cfif true>
    <cfset x = "dog" />
</cfif>

<!--- My Way, 1 line --->
<cfset x = IfElse(true, "dog", "cat") />

 

Utility Function: GetInstanceName()

I can't claim the core of this function as my own, it comes from a presentation by Adam Lehman on ColdFusion server administration. I wrapped his code snippet into a function for reuse.

<cffunction name="GetInstanceName" access="public" output="false" returntype="string">        
    <cfreturn createObject("java", "jrunx.kernel.JRun").getServerName() />
</cffunction>

 

Utility Functions: ArrayRandomize()

A simple array randomization option. Note that although I use "CFMX_COMPAT" as the default randomization algorithm, you should probably always use "SHA1PRNG" to get better results.

<cffunction name="ArrayRandomize" access="public" output="true" returntype="array">
    <cfargument name="array" type="array" required="true" />
    <cfargument name="algorithm" type="string" required="false" default="CFMX_COMPAT" />
    
    <!---
        Fisher-Yates shuffle
        http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
     --->

    <cfset var i = ArrayLen(arguments.array) />
    <cfset var k = 0 />
    <cfset var temp = "" />
    
    <cfif i gt 0>
        <cfloop condition="i gt 0">
            <cfset k = RandRange(1, i, arguments.algorithm) />
            
            <cfset temp = arguments.array[i] />
            <cfset arguments.array[i] = arguments.array[k] />
            <cfset arguments.array[k] = temp />
            <cfset i = i - 1 />
        </cfloop>
    </cfif>
    
    <cfreturn arguments.array />
</cffunction>

 

Utility Function: RequireScript()

I'm not too happy with with this implementation, but I think it works out fairly well. Note that it uses the two functions in the related posts.

<cffunction name="RequireScript" access="public" output="false" returntype="void">
<cfargument name="script" type="string" required="true" />

<cfif NOT StructKeyExists(request, "__requirescript")>
        <cfset request["__requirescript"] = ArrayNew(1) />
    </cfif>
    <cfif NOT ArrayFind(request["__requirescript"], arguments.script)>
        <cfset ArrayAppend(request["__requirescript"], arguments.script) />
        <cfset IncludeScript(arguments.script) />
    </cfif>
    
<cfreturn />
</cffunction>

 

Utility Function: ListIsEmpty()

While this is a simple bit of code to write, I personally like the syntax of checking "something is empty" over writing len(something) gt 0.

<cffunction name="ListIsEmpty" access="public" output="false" returntype="boolean">
    <cfargument name="list" type="string" required="true" />
    <cfargument name="delimiters" type="string" required="false" default="," />
    
    <cfreturn (ListLen(arguments.list, arguments.delimiters) gt 0) />
</cffunction>

 

More Entries

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 on LinkedIn.