CFLib.org – Common Function Library Project

deleteEmptyFolder(path)

Last updated May 30, 2011

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

 
Rated 1 time(s). Average Rating: 4.0

Description:
Arguments: 1. Path : folder physical path for which you want to clean empty directory. Function return boolean value indicating directory empty or not and if empty it will be deleted.

Return Values:
Returns a boolean.

Example:

view plain print about
<cfset deleteEmptyFolder('#expandPath("/temp/")#')>

Parameters:

Name Description Required
path Path to delete (if empty). Yes

Full UDF Source:

view plain print about
<!---
 Delete empty folder from given path.
 
 @param path      Path to delete (if empty). (Required)
 @return Returns a boolean. 
 @author Pritesh (pritesh@thecfguy.com) 
 @version 1, May 30, 2011 
--->

<cffunction name="deleteEmptyFolder" access="public" output="false" returntype="boolean">
    <cfargument name="path" required="true" type="string" />
    <cfset var qList="">
    <cfset var qDir = "">
    <cfset var qFiles = "">
    <cfset var isEmpty = 1>
    <!--- List Directory --->
    <cfdirectory action="list" directory="#arguments.path#" recurse="no" name="qList">
    <!--- get sub directory list --->
    <cfquery name="qDir" dbtype="query">
        select * from qList where type='Dir'
    </cfquery>
    <!--- Call recursive function to check directory empty or not --->
    <cfloop query="qDir">
        <!--- If sub directory not empty mark current directory as not empty. --->
        <cfif not deleteEmptyFolder(qDir.directory & "\" & qDir.name)>
            <cfset isEmpty=0>
        </cfif>
    </cfloop>

    <!--- Check for file exists in current directory --->
    <cfquery name="qFiles" dbtype="query">
        select * from qList where type='File'
    </cfquery>
    <!--- If file exists mark as not empty --->
    <cfif qFiles.recordCount gt 0>
        <cfset isEmpty = 0>
    </cfif>

    <!--- If current directory empty then delete it --->
    <cfif isEmpty>
        <cfdirectory action="delete" recurse="false" directory="#arguments.path#">
    </cfif>
    <!--- Return empty status for current directory --->
    <cfreturn isEmpty>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson