Find number of files with a specific extension, in all subdirectories

前端 未结 7 1759
执念已碎
执念已碎 2020-12-03 07:16

Is there a way to find the number of files of a specific type without having to loop through all results inn a Directory.GetFiles() or similar method? I am looking for somet

相关标签:
7条回答
  • 2020-12-03 08:09

    You can use this overload of GetFiles:

    Directory.GetFiles Method (String, String, SearchOption)

    and this member of SearchOption:

    AllDirectories - Includes the current directory and all the subdirectories in a search operation. This option includes reparse points like mounted drives and symbolic links in the search.

    GetFiles returns an array of string so you can just get the Length which is the number of files found.

    0 讨论(0)
  • 2020-12-03 08:09

    I was looking for a more optimized version. Since I haven't found it, I decided to code it and share it here:

        public static int GetFileCount(string path, string searchPattern, SearchOption searchOption)
        {
            var fileCount = 0;
            var fileIter = Directory.EnumerateFiles(path, searchPattern, searchOption);
            foreach (var file in fileIter)
                fileCount++;
            return fileCount;
        }
    

    All the solutions using the GetFiles/GetDirectories are kind of slow since all those objects need to be created. Using the enumeration, it doesn't create any temporary objects (FileInfo/DirectoryInfo).

    see Remarks http://msdn.microsoft.com/en-us/library/dd383571.aspx for more information

    0 讨论(0)
  • 2020-12-03 08:12

    The slickest method woud be to use linq:

    var fileCount = (from file in Directory.EnumerateFiles(@"H:\iPod_Control\Music", "*.mp3", SearchOption.AllDirectories)
                        select file).Count();
    
    0 讨论(0)
  • 2020-12-03 08:15

    You should use the Directory.GetFiles(path, searchPattern, SearchOption) overload of Directory.GetFiles().

    Path specifies the path, searchPattern specifies your wildcards (e.g., *, *.format) and SearchOption provides the option to include subdirectories.

    The Length property of the return array of this search will provide the proper file count for your particular search pattern and option:

    string[] files = directory.GetFiles(@"c:\windows\system32", "*.dll", SearchOption.AllDirectories);
    
    return files.Length;
    

    EDIT: Alternatively you can use Directory.EnumerateFiles method

    return Directory.EnumerateFiles(@"c:\windows\system32", "*.dll", SearchOption.AllDirectories).Count();
    
    0 讨论(0)
  • 2020-12-03 08:15

    Using recursion your MagicFindFileCount would look like this:

    private int MagicFindFileCount( string strDirectory, string strFilter ) {
         int nFiles = Directory.GetFiles( strDirectory, strFilter ).Length;
    
         foreach( String dir in Directory.GetDirectories( strDirectory ) ) {
            nFiles += GetNumberOfFiles(dir, strFilter);
         }
    
         return nFiles;
      }
    

    Though Jon's solution might be the better one.

    0 讨论(0)
  • 2020-12-03 08:15

    Someone has to do the iterating part.

    AFAIK, there is no such method present in .NET already, so I guess that someone has to be you.

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