CFLib.org – Common Function Library Project

deleteDirectory(directory [, recurse])

Last updated July 28, 2005
Download UDF

author

Rick Root                                         Rick Root

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

Description:
This UDF allows you to delete a directory and all of it's contents. Note - in CFMX7, this UDF is not necessary.

Return Values:
Return a boolean.

Example:

deleteDirectory("/home/webworksllc/www/test/foo2","True")

Parameters:

Name Description Required
directory The directory to delete. Yes
recurse Whether or not the UDF should recurse. Defaults to false. No

Full UDF Source:

<!---
Recursively delete a directory.

@param directory      The directory to delete. (Required)
@param recurse      Whether or not the UDF should recurse. Defaults to false. (Optional)
@return Return a boolean.
@author Rick Root (rick@webworksllc.com)
@version 1, July 28, 2005
--->

<cffunction name="deleteDirectory" returntype="boolean" output="false">
    <cfargument name="directory" type="string" required="yes" >
    <cfargument name="recurse" type="boolean" required="no" default="false">
    
    <cfset var myDirectory = "">
    <cfset var count = 0>

    <cfif right(arguments.directory, 1) is not "/">
        <cfset arguments.directory = arguments.directory & "/">
    </cfif>
    
    <cfdirectory action="list" directory="#arguments.directory#" name="myDirectory">

    <cfloop query="myDirectory">
        <cfif myDirectory.name is not "." AND myDirectory.name is not "..">
            <cfset count = count + 1><cfdump var="#myDirectory#">
            <cfswitch expression="#myDirectory.type#">
            
                <cfcase value="dir">
                    <!--- If recurse is on, move down to next level --->
                    <cfif arguments.recurse>
                        <cfset deleteDirectory(
                            arguments.directory & myDirectory.name,
                            arguments.recurse )
>

                    </cfif>
                </cfcase>
                
                <cfcase value="file">
                    <!--- delete file --->
                    <cfif arguments.recurse>
                        <cffile action="delete" file="#arguments.directory##myDirectory.name#">
                    </cfif>
                </cfcase>            
            </cfswitch>
        </cfif>
    </cfloop>
    <cfif count is 0 or arguments.recurse>
        <cfdirectory action="delete" directory="#arguments.directory#">
    </cfif>
    <cfreturn true>
</cffunction>

Search CFLib.org


Latest Additions

Raymond Compton Raymond Compton added
structBlend
19 day(s) ago

Duncan Duncan added
IsZIPUK
19 day(s) ago

Todd Sharp Todd Sharp added
getTagContentAll
25 day(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Created by Raymond Camden / Design by Justin Johnson