Click here to Skip to main content
Click here to Skip to main content

Delete specific folders using recursion

By , 27 Jul 2012
 

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

pramodhegde88
Software Developer
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionThis is potentially doing more work than necessarymemberMatt T Heffron27 Jul '12 - 14:15 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 27 Jul 2012
Article Copyright 2012 by pramodhegde88
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid