CFLib.org – Common Function Library Project

deleteDirectory(directory[, recurse])

Last updated September 19, 2014

author

Rick Root

Version: 1 | Requires: CF6 | 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, September 19, 2014 
--->
<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>
			<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

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson