Home > other >  Best way to return an enumeration of files with certain extensions
Best way to return an enumeration of files with certain extensions

Time:07-22

I need to create an enumeration of spreadsheets based on their file extensions, I need two options either recursive and no recursive and I need to return the enumeration from my enumeration. I have this code

using System;
using System.IO;
using System.IO.Enumeration;
using System.Collections.Generic;
using Enumerate_org = System.Collections.Generic.IEnumerable<string>;

        public IEnumerable<T> Enumerate_Original<T>(string argument1, string argument3)
        {
            Enumerate_org<T> org_enumeration = new Enumerate_org<T>();

            // Recurse enumeration of original spreadsheets from input directory
            if (argument3 == "Recurse=Yes")
            {
                org_enumeration = Directory.EnumerateFiles(argument1, "*.*", SearchOption.AllDirectories)
                .Where(file => file.EndsWith(".fods") || file.EndsWith(".ods") || file.EndsWith(".ots") || file.EndsWith("xla") || file.EndsWith(".xls") || file.EndsWith(".xls") || file.EndsWith(".xlt") || file.EndsWith(".xlam") || file.EndsWith(".xlsb") || file.EndsWith(".xlsm") || file.EndsWith(".xlsx") || file.EndsWith(".xltm") || file.EndsWith(".xltx"))
                .ToList();

                return org_enumeration;
            }

            // No recurse enumeration
            else
            {
                org_enumeration = Directory.EnumerateFiles(argument1, "*.*", SearchOption.TopDirectoryOnly)
                .Where(file => file.EndsWith(".fods") || file.EndsWith(".ods") || file.EndsWith(".ots") || file.EndsWith("xla") || file.EndsWith(".xls") || file.EndsWith(".xls") || file.EndsWith(".xlt") || file.EndsWith(".xlam") || file.EndsWith(".xlsb") || file.EndsWith(".xlsm") || file.EndsWith(".xlsx") || file.EndsWith(".xltm") || file.EndsWith(".xltx"))
                .ToList();

                return org_enumeration;
            }

        }

But I get the error

The using alias 'Enumerate_org' cannot be used with type arguments
  1. What am I doing wrong?

  2. I have an array of file extensions that are accepted. How to replace

file.EndsWith("a specific extension")

with the same search but trying to match any of the entries in the extensions array?

CodePudding user response:

I suggest using Path class in order to obtain extension:

// Since we enumerate files' names, we return IEnumerable<string>
public static IEnumerable<string> Enumerate_Original(string argument1,
                                                     string argument3) {
  var extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
    //TODO: add all required extensions here
    ".fods",
    ".ods",
  };

  return Directory
    .EnumerateFiles(argument1, 
                    "*.*", 
                    argument3 == "Recurse=Yes" 
                      ? SearchOption.AllDirectories 
                      : SearchOption.TopDirectoryOnly)
    .Where(file => extensions.Contains(Path.GetExtension(file)));
}

CodePudding user response:

I can't test this right now but this should get you there...

using Enumerate_org = System.Collections.Generic; // why do this? why not just "using System.Collections.Generic"? 
:
var org_enumeration = new List<string>(); // question #1
:
... Where(file => ExtensionsList.Contains(Path.GetExtension(file)))... // question #2

There are other flaws like distinguishing between interface and type/class and the fact that you (probably) don't want the .ToList() (or you might as well return List<string>).

CodePudding user response:

AlanK got me on the right track and I turned the IEnumerable into a list instead. The code is now:

 public List<string> Enumerate_Original(string argument1, string argument3)
        {
            
            var org_enumeration = new List<string>();

            // Recurse enumeration of original spreadsheets from input directory
            if (argument3 == "Recurse=Yes")
            {
                org_enumeration = (List<string>)Directory.EnumerateFiles(argument1, "*.*", SearchOption.AllDirectories)
                    .Where(file => file_format.Contains(Path.GetExtension(file)))
                    .ToList();

                return org_enumeration;
            }

            // No recurse enumeration
            else
            {
                org_enumeration = (List<string>)Directory.EnumerateFiles(argument1, "*.*", SearchOption.TopDirectoryOnly)
                   .Where(file => file_format.Contains(Path.GetExtension(file)))
                   .ToList();

                return org_enumeration;
            }

        }
  • Related