Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Delete specific folders using recursion

0.00/5 (No votes)
27 Jul 2012 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.


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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here