Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5939307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:48:34+00:00 2026-05-22T15:48:34+00:00

I am working on project where I need to delete all empty folder (directories)

  • 0

I am working on project where I need to delete all empty folder (directories) from specific location. I am sure there is no direct way to find empty director in folder. I just write below method to delete empty folder in hierarchy. Can anyone have better solution?

<cffunction name="deleteEmptyFolder" access="public" output="true" 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:48:35+00:00Added an answer on May 22, 2026 at 3:48 pm
    <cffunction name="deleteEmptyFolders" output="false">
        <cfargument name="path" required="true" />
        <cfset var subfolders = "" />
        <cfdirectory name="subfolders" action="list" directory="#path#" type="dir" />
        <cfloop query="subfolders">
            <cfset deleteEmptyFolders("#path#/#subfolders.name#") />
        </cfloop>
        <cftry>
            <cfdirectory action="delete" directory="#path#" />
            <cfcatch></cfcatch>
        </cftry>
    </cffunction>
    

    Edit: The error catch is there just to make the code simpler and avoid another file list calls. You can also use this instead…

    <cfdirectory name="files" action="list" directory="#path#" />
    <cfif not files.recordcount>
        <cfdirectory action="delete" directory="#path#" />
    </cfif>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working a project where I need to generate a series of classes
I'm working on a project were we need more performance. Over time we've continued
I'm working on a project where I need the following. WCF service on the
I am working on a project where I need to create a boundary around
I am working on a project where I need to deal at the byte
I've been working on a project where I need to iterate through a collection
I'm currently working on a pet project and need to do C++ development on
I am currently working on a project where users need to be able to
I'm working on a project where I have 2 web services that need the
I'm working on a database project and I need to create Outlook 2007 plugin

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.