Click here to Skip to main content
11,479,170 members (64,113 online)
Click here to Skip to main content

Delete specific folders using recursion

, 27 Jul 2012 CPOL 6.6K 2
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

pramodhegde88
Technical Lead
India India
No Biography provided

Comments and Discussions

 
SuggestionThis is potentially doing more work than necessary Pin
Matt T Heffron27-Jul-12 15:15
memberMatt T Heffron27-Jul-12 15:15 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 27 Jul 2012
Article Copyright 2012 by pramodhegde88
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid