0

I want to delete all the files in a folder, and delete all the files in all its sub folders, and sub sub folders, etc, BUT I do not want to delete the folders themselves.

What would be the simplest way to do this?

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
bigcat42
  • 247
  • 2
  • 7
  • @Woot4Moo: No, because this specifically asks to NOT delete folders in the directory. – Jeff Apr 19 '13 at 18:35
  • 3
    @Jeff I would guess it's due mainly to the fact that he really just needed to learn how to iterate through files and there is plenty of information on how to do that. However, it's a pretty excessive. People on here just love to bash beginning programmers/SO users rather than help them. – evanmcdonnal Apr 19 '13 at 18:39
  • @Jeff and yet that answer is also contained in the post. OP should search instead of inundating the site with questions that were already answered. – Woot4Moo Apr 19 '13 at 18:39
  • @evanmcdonnal no the issue is OP didn't provide any code. How am I as a developer supposed to help someone who has not proven the most remote baseline of understanding. – Woot4Moo Apr 19 '13 at 18:40
  • @Jeff As the text of the downvote tooltip states, "The question does not show any research effort." The OP has shown no effort spent solving the problem, or attempting to find existing material on the topic. – Servy Apr 19 '13 at 18:41
  • 2
    @Woot4Moo yeah I don't disagree with that. I understand your reason for downvoting. It's just that after it has a couple downvotes I'm not going to keep piling them on. I'm sure the OP already feels dumb after 2 downvotes (I know I do when I get downvoted), they don't need 10. – evanmcdonnal Apr 19 '13 at 18:43
  • Ouch. Yes, I'm a beginner. I did attempt searching for a solution, and it should be a very simple problem to solve, but I've had trouble finding help for this specific case of deleting files but not directories. – bigcat42 Apr 19 '13 at 18:48

2 Answers2

8
foreach (var file in Directory.EnumerateFiles("path", "*", System.IO.SearchOption.AllDirectories))
{
    //TODO consider error handling
    File.Delete(file);
}
Servy
  • 202,030
  • 26
  • 332
  • 449
5
  static void DirSearch(string sDir)
   {
       try
       {
           foreach (string d in Directory.GetDirectories(sDir))
           {
               foreach (string f in Directory.GetFiles(d))
               {
                   //Delete files, but not directories
                   File.Delete(f);
               }
               //Recursively call this method, so that each directory
               //in the structure is wiped
               DirSearch(d);
           }
       }
       catch (System.Exception excpt)
       {
           Console.WriteLine(excpt.Message);
       }
   }
Jeff
  • 2,835
  • 3
  • 41
  • 69
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • 2
    Rather than manually traversing using your own recursive function you can just use the library method, which can do it somewhat more efficiently (and without using recursion, so eliminating the possibility of a Stack Overflow Exception). – Servy Apr 19 '13 at 18:35
  • Also, if you wanted to continue deleting files even if one fails to delete, you can move the try/catch to the inner `foreach`. You'd probably want to log the failures then though, and return a status. – Matthew Watson Apr 19 '13 at 18:37
  • @Servy yeah that is a fine solution, perhaps better. I'd rather show how it actually works though. I don't like super high level abstractions that just magically work and I don't think it's a good idea to suggest beginning programmers use them. – evanmcdonnal Apr 19 '13 at 18:37
  • @evanmcdonnal If your method worked equally well, then that's fine. When it doesn't it's better to suggest the library method and show a custom "similar" method just for academic purposes. Actually using a method such as yours (as is) in production code should be discouraged. – Servy Apr 19 '13 at 18:39