0

I have a foreach statement that is part this function:

try
{
  string pathToFiles = sourceTB.Text;
  DirectoryInfo dirInfo = new DirectoryInfo(pathToFiles);
  int i = 0; 

  foreach (var files in dirInfo.EnumerateFiles())
  {
    //Do stuff here
  }
  MessageBox.Show("Process Successful", "Completed");
}
catch
{
  MessageBox.Show("Process failed", "Failed");
}

I would like to be able to code it so that it will read the entire contents of the selected folder (this is working fine), but I am trying to make it so it will skip any files with a certain file extension.

For Example if every file in the folder was a .txt file and I wanted to leave out the .jpg files.

I have tried a few ways to achieve this such as:

var allFilesFromFolder = Directory.GetFiles(pathToFiles);
var filesToExclude = Directory.GetFiles(pathToFiles, "*.jpg");
var filetoInclude = allFilesFromFolder.Except(filesToExclude);

and this:

var files = Directory.GetFiles(pathToFiles).Where(name => !name.EndsWith(".jpg"));

but both of these bring errors into to the code and wont fit in the foreach loop when cycles through the files.

Is there any way to achieve this?

  • 3
    What error they bring in the code? Please explain – Steve Jul 14 '17 at 15:47
  • 1
    What exactly is going wrong? You code (at least the second variant) seems ok. – Sergey.quixoticaxis.Ivanov Jul 14 '17 at 15:47
  • 1
    Possible duplicate of [Directory.GetFiles of certain extension](https://stackoverflow.com/questions/13301053/directory-getfiles-of-certain-extension) – Vladimir Arustamian Jul 14 '17 at 15:48
  • Works fine for me. I don't see where you are getting an error. – Bob G Jul 14 '17 at 15:49
  • The way you're trying to do it should be fine, however another approach is to keep your original code `foreach (var files in dirInfo.EnumerateFiles())` and then put and `if` inside the loop to check the file extension and skip it with `continue` it if it is jpg. – Racil Hilan Jul 14 '17 at 15:50

2 Answers2

3

You can prepare a list of extensions you don't want to process and then use that list while enumerating the FileInfo class returned by a DirectoryInfo.EnumerateFiles

List<string> excluded = new List<string>() {".jpg", ".png"};
foreach (var file in dirInfo.EnumerateFiles().Where(x => !excluded.Contains(x.Extension))
{
   //Do stuff here
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Works perfect. Thankyou! –  Jul 14 '17 at 15:58
  • 1
    Just a side note about variable naming, it should be singular `var file`. I know the OP has it plural, but that's either a typo or reflects incorrect understanding of the `foreach` loop, so it would be better if fixes in the accepted answer. – Racil Hilan Jul 14 '17 at 16:05
  • thanks for the note @RacilHilan I am trying to improve the cleanliness of my code for others to read so I appreciate your post. –  Jul 14 '17 at 16:12
  • 1
    A final note. You can add a ToLower() to the Extension property to be sure to match also files with an upper case extension – Steve Jul 14 '17 at 17:03
0

The way you're trying to do it should work fine, but you can also include a pattern in your original code, for instance this will give you all text files:

foreach (var file in dirInfo.EnumerateFiles("*.txt"))
{
  //Do stuff here
}

Another approach is to keep your original code and then put and check the extension inside:

foreach (var file in dirInfo.EnumerateFiles("*.txt"))
{
  if(file.Extension == ".jpg")
    continue;

  //Do stuff here
}
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55