1. Computing & Technology

Discuss in my forum

Challenge: Create a Custom Delphi Function: RemoveEmptyFolders

Delphi Challenge: Create a Function to Remove Empty Sub-Folders

By , About.com Guide

RemoveEmptyFolders Preview

RemoveEmptyFolders Preview

Delphi programming challenges / exercises are designed to help you refactor your old code to make it more efficient; get new ideas on how to code faster / better Delphi code; help you solve a particular Delphi programming task and of course: to have fun while coding in Delphi :)

Challenge: Remove/Delete Empty Folders

Your challenge is to code a custom Delphi function with the following signature:
 procedure RemoveEmptyFolders(const rootFolder : string) ;
 
RemoveEmptyFolders takes 1 path name (directory) and should remove (delete) all empty folders / sub-folders (any sub level) under the provided "rootFolder".

A folder is empty if it does not have any files or sub folders.

For example: given the following folder/file structure:

 ROOTFOLDER
 - FOLDER-A
 - FOLDER-B
 - - file-b-1.ext
 - - file-b-2.ext
 - - SUB-FOLDER-B
 - - - SUB-FOLDER-B-1
 - FOLDER C
 - - file-c-1.ext
 
Note: folders are in UPPER CASE (ex: SUB-FOLDER-B-1), files are in lower case (ex: file-c-1.ext).

In the above structure, folders "FOLDER-A" and "SUB-FOLDER-B-1" are empty. The result of a call to RemoveEmptyFolders should be:

 ROOTFOLDER
 - FOLDER-B
 - - file-b-1.ext
 - - file-b-2.ext
 - FOLDER C
 - - file-c-1.ext
 
 
Note: Since "SUB-FOLDER-B-1" was empty, after removing, the folder "SUB-FOLDER-B" is also empty, and should be also removed.

Here are a few hints:

Enter Challenge

Note: This is for glory only. About.com does not permit prizes to be given.

To "win" the challenge you obviously need to submit a function that solves the given problem. Also, make sure the code is "nice", has no memory leaks and preferably does not have hundreds of lines.

Selected entries will be listed. A condition of entry is that you allow your source code to be published on this website, with full credits to you as the author.

RemoveEmptyFolders : Closed!

Note: this challenge is closed. Several valid entries have been submitted before the challenge was closed.

RemoveEmptyFolders : Accepted Entries

Here's the list of the accepted entries. 5 entries were selected.

The "trick" in this challenge was to create a recursive function which goes down to sub-folder level to see if a sub folder is empty to delete it. Once all empty sub-folders are deleted, if the parent folder is empty it gets deleted.

Common to entries is the usage of the RTL routines.

If RemoveDir was used to delete an empty folder, there was no need to check if the folder is actually empty, since RemoveDir would simply not delete a folder if it is not empty.

A winning entry would look something like (pseudo code):

 procedure RemoveEmptyFolders(rootFolder)
 begin
   for each subFolder in "Get-Sub-Folders(rootFolder)" do
     RemoveEmptyFolders(subFolder) ;
   RemoveDir(rootFolder) ;
 end;
 

Note: the list is in no particular order.

 ExtractBasePath by Jur Huisman
 ExtractBasePath by Christian Graack
 ExtractBasePath by Willie Geldenhuys
 ExtractBasePath by Peter Luijes
 ExtractBasePath by David Reed. WINNER - 5 "fame and glory" points! :)
 
Q: How do you select the winning entry?
A: Firstly, the submitted code must solve the given problem. Secondly, the code has no memory leaks, is clean and simple (as much as it can be), no unnecessary code lines. Finally, from a pure subjective point of view (mine) the winning entry is the one I like the most :)

Note: before the challenge was closed 12 entries were accepted, the implementations of those 7 not listed were much like the 5 listed above. Accepted but not listed: Paul Bennett, Korhan Taþçýkar, Andreas Lauß, Jaison Joseph, Martin Richer, Mark Billig and Mehmet Can KANPOLAT.

Participate in active Delphi programming challenges

Readers Respond: Your Delphi Programming Challenges

©2012 About.com. All rights reserved.

A part of The New York Times Company.