CFLib.org – Common Function Library Project

deleteDirectory(directory [, recurse])

Last updated July 28, 2005

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

 
Rated 0 time(s). Average Rating: 0

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:

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

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

Search CFLib.org


Latest Additions

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

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

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

Ben Forta Ben Forta added
GetHostAddress
30 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