C# Recurse Directories using Directory.GetFiles and search pattern

前端 未结 6 1849
时光说笑
时光说笑 2021-01-21 02:42

I want to find all excel files within a directory structure using recursion. The problem is, the search pattern used in Directory.GetFiles only allows a single extension at a t

相关标签:
6条回答
  • 2021-01-21 03:10

    If you only want to get all excel files, use the pattern ".xl".

    Otherwise I would suggest to call Directory.GetFiles without pattern and filter the matching extensions by hand.

    0 讨论(0)
  • 2021-01-21 03:16

    In .NET every version there is SearchOption.TopDirectoryOnly and SearchOption.AllDirectories

    In .NET 4 you could very efficiently do e.g.:

            var regex = new Regex(@"\d+", RegexOptions.Compiled);
    
            var files = new DirectoryInfo(topdir)
    
                .EnumerateFiles("*.*", SearchOption.AllDirectories)
    
                .Where(fi => regex.IsMatch(fi.Name));
    

    (This example filters for files having two digits in the name)

    To emulate this, write a recursive enumerator method (yield return) to return all files, and filter the result like so:

     IEnumerable<FileInfo> Recurse(string topdir)
     { 
          // for each GetFiles() array element
          //      if is_not_dir yield return
          //      else Recurse(subdir)          
     }
    
     var filtered = Recurse.Where(fi => regex.IsMatch(fi.Name));
    

    HTH

    0 讨论(0)
  • 2021-01-21 03:19

    In .NET 4 there is an extra overload that allows the inclusion of subfolders

    EDIT oops I did not read the question very well...

    Have a look here

    0 讨论(0)
  • 2021-01-21 03:24

    To loop through directory and sub directories, No Matter how much sub folder or files are, you can get the files into an array. You can specify the type file, Jpeg, Excel, Msword what ever you want in the extension section.

    string [] Excel_Files;
    String path = "what ever is your path";
    
    Files=  Directory.GetFiles(Path, "*.XL", SearchOption.AllDirectories).Select(x => Path.GetFileName(x)).ToArray();
    

    or To specify Multiple search option for different file extensions you can do like this:

    public string[] getFiles(string SourceFolder, string Filter, 
     System.IO.SearchOption searchOption)
    {
    
    ArrayList alFiles = new ArrayList();
    
     string[] MultipleFilters = Filter.Split('|');
    
     foreach (string FileFilter in MultipleFilters)
     {
           alFiles.AddRange(Directory.GetFiles(SourceFolder, FileFilter, searchOption));
     }
    
     return (string[])alFiles.ToArray(typeof(string));
    }
    
    public void button_click()
    {
    
    string[] sFiles = getFiles(Server.MapPath("~/"), 
     "*.gif|*.jpg|*.png|*.bmp|*.XL|*.PNG", 
     SearchOption.AllDirectories);
    
    foreach (string FileName in sFiles)
    {
     Response.Write(FileName + "<br />");
    }
    }
    
    0 讨论(0)
  • 2021-01-21 03:31

    I think the second alternative is more efficient. Loop through every file with the following pattern: .xl, then narrow the list looking for specific endings.

    Something like:

    foreach (String f in Directory.GetFiles(path, "*.xl*", SearchOption.AllDirectories))
    {
        if (HasSomeExcelExtension(f))
            files .Add(f);
    }
    

    You could use the EndsWith method to check "f" against each extension, or extract the extension using the Path.GetExtension method and look it up in a hashtable containing the desired extensions.

    just my $.02 hth

    0 讨论(0)
  • 2021-01-21 03:35

    Modify your recursive loop and have a list of patterns. eg

    static private void walk(String name)
    {
        try
        {
            foreach (String pattern in Patterns)
            {
                foreach (String f in Directory.GetFiles(name, pattern))
                {
                    Console.WriteLine(f);
                 } 
            }
                foreach (String d in Directory.GetDirectories(name))
            {
                walk(d);
            }
        }
        catch
        {
    
        }
    

    }

    0 讨论(0)
提交回复
热议问题