Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Remove all empty subdirectories

I have a task to clean up a large number of directories. I want to start at a directory and delete any sub-directories (no matter how deep) that contain no files (files will never be deleted, only directories). The starting directory will then be deleted if it contains no files or subdirectories. I was hoping someone could point me to some existing code for this rather than having to reinvent the wheel. I will be doing this using C#.

like image 880
Jay Avatar asked May 11 '10 14:05

Jay


1 Answers

Using C# Code.

static void Main(string[] args) {     processDirectory(@"c:\temp"); }  private static void processDirectory(string startLocation) {     foreach (var directory in Directory.GetDirectories(startLocation))     {         processDirectory(directory);         if (Directory.GetFiles(directory).Length == 0 &&              Directory.GetDirectories(directory).Length == 0)         {             Directory.Delete(directory, false);         }     } } 
like image 79
Ragoczy Avatar answered Sep 28 '22 08:09

Ragoczy