views:

65

answers:

5

hello all, i'm working a feature for a application of mine that on button click it scans the "C:\" drive (and all sub directory's, read-only or not.), and deletes all files with specific file extensions. how would i go about doing this? I'm sure a list or a array would be used... but thats about all i know.

Please .Net framework 2.0 ONLY!

+1  A: 

Walk the directory tree. The requisite code is described here.

Marcelo Cantos
I feel those examples are pretty dated, since the API's listed, can recurse for you by passing in an extra argument. So why write an extra 50 lines of code when you can just pass in an extra parameter?
C Johnson
@CJohnson: Though if you read the link you'll see that the reason for it being done as step by step is to make it better at handling errors (which I didn't think about for my answer either). So thinking about it, it might be a lot of code, but this is probably the safest way.
ho1
+1  A: 
foreach (string filename in Directory.EnumerateFiles(@"C:\", "*.xxx", SearchOption.AllDirectories)
{
    File.Delete(filename);
}
Paul Ruane
`EnumerateFiles` doesn't recurse.
Marcelo Cantos
@Marcelo Cantos: it does in my test. Have you actually tried it?
Paul Ruane
The good thing about EnumerateFiles is it returns directories right away, not after it has recursed through all the directories. It is ideal for getting results right away. Otherwise GetFiles does the same thing, but only returns after all the files have been retrieved - Which is you have thousands of directories could take a while. So this method is useful if you want to do a multi-threaded operation over LOTS of directories.
C Johnson
`EnumerateFiles` is not part of .Net 2.0 as far as I'm aware.
ho1
yes please .Net 2.0 only
NightsEVil
+1  A: 
foreach (String file in Directory.GetFiles("c:\\","*.iddqd", SearchOption.AllDirectories) )
        File.Delete (file);
Orsol
I'm pretty positively sure that var was NOT in .NET 2.0.
C Johnson
You right, I edited my answer
Orsol
I think `var` is a compiler dependant rather than a framework dependant, so it could be used in .Net 2.0 if you're compiling it in VS2008 or later so in theory you're wrong (but I'd assume that when the query mentions .Net 2.0 it's quite likely what's actually meant is VS2005 with .Net 2.0 so in practice your comment is probably correct).
ho1
that bears some looking into. We should query if they fellow is using VS 2005 or not.
C Johnson
i am using VS 2010 express.
NightsEVil
+1  A: 

I think the following code would work:

using System.IO;

...


string[] extensions = { "*.apa", "*.dip", "*.ep" }; // whatever extensions you care about
foreach (string ext in extensions)
{
     foreach (string file in Directory.GetFiles(@"c:\", ext, SearchOption.AllDirectories))
     {
          File.SetAttributes(file, FileAttributes.Normal);
          File.Delete(file);
     }
}
ho1
best looking answer iv seen but so far i'm getting a error like this "access to path 'c:\windows\system32\logfiles\wmi\rtbackup' is denied. Whats wrong?
NightsEVil
@NightsEvil: Yes, I had forgotten about that it's probably not a good idea to do AllDirectories option if you want to search the whole system drive, because it will fail on some directories and then it'll fail the whole operation. Instead you'll have to do the recursive way as described in the article in Marcelo Cantos' answer where if it fails with one directory it'll just ignore that exception and continue with the next.
ho1
could that be worked in with how you have done it just changed? cause out of all the answers i like yours the best.
NightsEVil
@NightsEvil: Sure, just start with that sample, and then when it writes out the filename to the console you instead call `SetAttributes` and `Delete` as in my code above. And then you just change the the `WalkDirectoryTree` to accept a second parameter which will be the extension and use that extension insead of the "*.*" and you then just call the function once for each extension. BTW, be careful when you're doing this so that you don't accidently do a delete on "*.*"...
ho1
im looking at the example and im having a hard time seeing where it calls the files to the console and all that. and im trying to have this in a button click for now (later id like it to be in combo drop down box with the list of logical drives and have the code go and scan that drive.
NightsEVil
+1  A: 

try this:

 DirectoryInfo directoryInfo = new DirectoryInfo(@"directory path");
        foreach (var f in directoryInfo.GetFiles("*.*", SearchOption.AllDirectories))
        {
            f.Delete();
        }
anishmarokey