2

I have the following solution to delete a single file type with only one extension; however I am trying to find a way to incorporate multiple file extensions, for example: .wma, .mp3, .wav

DirectoryInfo di = new DirectoryInfo(@"C:\");
FileInfo[] files = di.GetFiles("*.mpeg")
                     .Where(p => p.Extension == ".mpeg").ToArray();
foreach (FileInfo file in files)
    try
    {
        file.Attributes = FileAttributes.Normal;
        File.Delete(file.FullName);
    }
    catch { }
neminem
  • 2,658
  • 5
  • 27
  • 36
dames
  • 1,421
  • 8
  • 35
  • 55

4 Answers4

10

Well that's just a matter of changing your condition to be "where the extension is any one of a set" and removing the pattern from the GetFiles call. Also note that you don't need to call ToArray() - just iterate over the results:

var extensions = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
    { ".wma", ".mp3", ".wav" };

var files = new DirectoryInfo(@"c:\").GetFiles()
                                     .Where(p => extensions.Contains(p.Extension));
foreach (var file in files)
{
    // try/catch if you really need, but I'd recommend catching a more
    // specific exception
    file.Attributes = FileAttributes.Normal;
    File.Delete(file.FullName);    
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank You, A tangent question, could this method be applied to the copying of files? – dames Dec 15 '14 at 17:32
  • @dames: Well it's just selecting the filenames, really - what you do with them is up to you. – Jon Skeet Dec 15 '14 at 17:51
  • Well I would like to select the files and move them to specific locations, however doing it in this approach where I incorporate all the formats – dames Dec 15 '14 at 17:57
  • @dames: It's not really clear what you're asking for - but I suggest you try what you can and then ask a new and specific question if you really need to. – Jon Skeet Dec 15 '14 at 17:59
6

You can store the allowed extensions in a collection and use Enumerable.Contains:

string[] allowedExtensions = { ".mpeg", ".wma", ".mp3", ".wave" };
FileInfo[] files = di.EnumerateFiles()
 .Where(p => allowedExtensions.Contains(p.Extension, StringComparer.InvariantCultureIgnoreCase))
 .ToArray();

Note that i've used DirectoryInfo.EnumerateFiles which is probably more efficient in this case since it does not need to load all file-names into memory(as DirectoryInfo.GetFiles) before it can start processing. StringComparer.InvariantCultureIgnoreCase should be self-explaining.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Felt the need to point out that I thought you were saying GetFiles loads all the files into memory before it returns but it just returns the filenames. – Sam Nov 30 '16 at 13:43
4

You can have extension in an array and then get all files from the directory and compare each extension like:

string[] fileExtensions = new[] { ".wma", ".mp3", ".wave" };
DirectoryInfo di = new DirectoryInfo(@"C:\");
FileInfo[] files = di.GetFiles()
                     .Where(p => fileExtensions.Contains(p.Extension)).ToArray();

If you want case insensitive comparison then use:

FileInfo[] files = di.GetFiles()
                     .Where(p => fileExtensions.Contains
                    (p.Extension, StringComparer.InvariantCultureIgnoreCase)).ToArray();
Habib
  • 219,104
  • 29
  • 407
  • 436
0
di.GetFiles().Where(
    p => p.Extension == ".mpeg" || 
    p.Extension == ".mp3" || 
    p.Extension == ".wave");
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111