Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C#
Tip/Trick

Delete specific folders using recursion

Rate me:
Please Sign up or sign in to vote.
4.83/5 (4 votes)
27 Jul 2012CPOL 21.3K   3   1
Delete all directories with a specific name inside a root directory

Introduction

This article gives idea how to use recursion to delete specific directory inside a root directory.

Image 1

Using the code

DeleteBinAndObj is the method used to delete all bin and obj folders inside ..Projects folder.

DeleteBinAndObj(@"C:\Users\Documents\Visual Studio 2010\Projects"); 

It gets the current folder info using System.IO.DirectoryInfo class and also keeps the information of sub-directories inside it in an array System.IO.DirectoryInfo[]. If there are any sub directories then the method call itself (recursion) by passing the current sub directory.

foreach (DirectoryInfo myItem in subfolders)
               DeleteBinAndObj(myItem.FullName); 

If there are no sub directories then the current directory name will be compared with the specific string and if it matches the current directory is deleted using Directory.Delete method

if(rootFolder.Name.Equals("bin") || rootFolder.Name.Equals("Bin") || rootFolder.Name.Equals("BIN")
||rootFolder.Name.Equals("obj") || rootFolder.Name.Equals("Obj") || rootFolder.Name.Equals("OBJ"))
     Directory.Delete(rootFolder.FullName, true); 

If a file inside a directory does not have enough permission to delete then the directory will not be deleted and Directory.Delete(...) method throws exception. So you can use a try-catch block to handle this situation.

try
{
    Directory.Delete(rootFolder.FullName, true);
        /// here you can log the deleted file FullName.
}
catch
{                    
    /// Access denied for few of the files inside the directory.
        return;
}   

The final code would look like this,

using System.IO;
namespace DeleteFolder
{
    class Program
    {
        static void Main(string[] args)
        {
            DeleteBinAndObj(@"C:\Users\Documents\Visual Studio 2010\Projects");
        }
        private static void DeleteBinAndObj(string rootPath)
        {
            DirectoryInfo rootFolder = new DirectoryInfo(rootPath);
            DirectoryInfo[] subfolders = rootFolder.GetDirectories();            
                        
            foreach (DirectoryInfo myItem in subfolders)
                DeleteBinAndObj(myItem.FullName);
            //delete the specific folder
            if (rootFolder.Name.Equals("bin") || rootFolder.Name.Equals("Bin") || rootFolder.Name.Equals("BIN") ||
                    rootFolder.Name.Equals("obj") || rootFolder.Name.Equals("Obj") || rootFolder.Name.Equals("OBJ"))
            {
                try
                {
                    Directory.Delete(rootFolder.FullName, true);
                    /// here you can log the deleted file FullName.
                }
                catch
                {                    
                    /// Access denied for few of the files inside the directory.
                    return;
                }
            }
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionThis is potentially doing more work than necessary PinPopular
Matt T Heffron27-Jul-12 14:15
professionalMatt T Heffron27-Jul-12 14:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.