How to count .xls / .xlsx files in the folder with C #?

3

I have a FolderBrowserDialog and when I select the folder it shows the files quantity (all, without filter). How do I do this length only with Excel files (.xls, .xlsx).

The following is the code below:

FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string[] files = Directory.GetFiles(fbd.SelectedPath);
System.Windows.Forms.MessageBox.Show("Arquivos na Pasta: " + files.Length.ToString(), "Alerta");
    
asked by anonymous 29.08.2014 / 15:21

2 answers

3

Ready in .NET in a simpler way, which can be done for both extensions is this:

FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string[] files = Directory.GetFiles(fbd.SelectedPath, "*.xls");
System.Windows.Forms.MessageBox.Show("Arquivos na Pasta: " + files.Length.ToString(), "Alerta");

The ideal would be to specify the two extensions individually but there is nothing ready in .NET. Anything that starts with "xls" and has one more character will be considered, so there may be an undesirable side effect, for example catching the "xlsh" files you eventually have, it's unlikely but it can happen. p>

A better option for .NET 4.5:

FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string[] files = Directory.EnumerateFiles(path)
                         .Where(file => file.ToLower().EndsWith("xls") ||
                                        file.ToLower().EndsWith("xlsx"))
System.Windows.Forms.MessageBox.Show("Arquivos na Pasta: " + files.Length.ToString(), "Alerta");

Retired from jgauffin's answer in OS .

If you can not use .NET 4.5 just change the EnumerateFiles method to GetFiles , the only disadvantage is performance since the second does not use lazy evaluation .

I still found this option:

Multiple Filters On Directory.GetFiles Method

Simplifying the example and adapting to your case:

FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string[] files = Directory.GetFiles(fbd.SelectedPath, "*.xls");
files.AddRange(Directory.GetFiles(fbd.SelectedPath, "*.xlsx"));
System.Windows.Forms.MessageBox.Show("Arquivos na Pasta: " + files.Length.ToString(), "Alerta");

Or simplifying and maintaining the generality:

public static string[] GetFiles(string sourceFolder, string filters, System.IO.SearchOption searchOption) {
   return filters.Split('|').SelectMany(filter =>
          System.IO.Directory.GetFiles(sourceFolder, filter, searchOption)).ToArray();
}

Source: Albert's answer in SO

Although I do not know if this is necessary. See the documentation indicating that "xlsx" is also caught.

  

When you use the asterisk wildcard character in a searchPattern such as "* .txt", the number of characters in the specified extension affects the search as follows:

     
  • If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. For example, "* .xls" returns both "book.xls" and "book.xlsx".

  •   
  • In all other cases, the method returns files that exactly match the specified extension. For example, " .ai" returns "file.ai" but not "file.aif".   When you use the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file? .Txt" returns just the first file, whereas a search pattern of "file .txt "returns both files.

  •   

As there is this behavior I do not know if any solution is ideal other than to do the crazy thing to create a own system of capture of the files and a filter in with the own criteria. Hardly worth the effort. So I think the second solution using LINQ turns out to be the best.

I placed it on GitHub for future reference.

    
29.08.2014 / 15:32
2

Using Linq to Objects

1 - Form

FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string[] files = System.IO.Directory.GetFiles(fbd.SelectedPath);
int length = files.Where(x => x.ToLower().EndsWith(".xls") || x.ToLower().EndsWith(".xlsx")).Count();
System.Windows.Forms.MessageBox.Show("Arquivos na Pasta: " + length.ToString(), "Alerta");

2 - Way

FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string[] files = System.IO.Directory.GetFiles(fbd.SelectedPath);
int length = files
                .Where(x => x.ToLower().Substring(x.Length - 4).Equals(".xls") || x.ToLower().Substring(x.Length - 5).Equals(".xlsx"))
                .Count();
System.Windows.Forms.MessageBox.Show("Arquivos na Pasta: " + length.ToString(), "Alerta");
    
29.08.2014 / 15:39