如何使用C#计算具有特定名称的子文件夹中的文件数?

时间:2017-08-07 22:19:45

标签: c# directory

我有一个如下所示的路径结构:

Main_Folder
      ***Sub_Folder1***
           **1) FOLDER_Y**
                 Folder1
                   picture1.png
                   picture2.png
                   picturen.png
                 Folder2
                   picture1.png
                   picture2.png
                   picturen.png
                 FolderN
                   picture1.png
                   picture2.png
                   picturen.png
           **2) FOLDER_X**
                 Folder1
                   picture1.png
                   picture2.png
                   picturen.png
                 Folder2
                   picture1.png
                   picture2.png
                   picturen.png
           **3) FOLDER_Z**
                 Folder1
                   picture1.png
                   picture2.png
                   picturen.png
                 Folder2
                   picture1.png
                   picture2.png
                   picturen.png
   ***Sub_Folder2***
           **1) FOLDER_Y**
                 Folder1
                   picture1.png
                   picture2.png
                   picturen.png
                 Folder2
                   picture1.png
                   picture2.png
                   picturen.png
                 FolderN
                   picture1.png
                   picture2.png
                   picturen.png
           **2) FOLDER_X**
                 Folder1
                   picture1.png
                   picture2.png
                   picturen.png
                 Folder2
                   picture1.png
                   picture2.png
                   picturen.png
           **3) FOLDER_Z**
                 Folder1
                   picture1.png
                   picture2.png
                   picturen.png
                 Folder2
                   picture1.png
                   picture2.png
                   picturen.png
   ***Sub_Folder3***
           **1) FOLDER_Y**
                 Folder1
                   picture1.png
                   picture2.png
                   picturen.png
                 Folder2
                   picture1.png
                   picture2.png
                   picturen.png
                 FolderN
                   picture1.png
                   picture2.png
                   picturen.png
           **2) FOLDER_X**
                 Folder1
                   picture1.png
                   picture2.png
                   picturen.png
                 Folder2
                   picture1.png
                   picture2.png
                   picturen.png
           **3) FOLDER_Z**
                 Folder1
                   picture1.png
                   picture2.png
                   picturen.png
                 Folder2
                   picture1.png
                   picture2.png
                   picturen.png
> Desired mock output: 
> FOLDER_Y # files: 27 
> FOLDER_X # files: 18
> FOLDER_Z # files: 18

我基本上希望用户输入主文件夹的路径,然后遍历每个子文件夹。然后从所有子文件夹输出ALL Folder_Y中的文件总数。对于Folder_X,Folder_Z也是如此。

我尝试了几种不成功的方法。

public void getFOLDER_Y(string strPath)
{
    string partialName = "FOLDER_Y";

    DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(strPath);
    FileInfo[] filesInDir = hdDirectoryInWhichToSearch.GetFiles("*" + partialName + "*.*");
    DirectoryInfo[] dirsInDir = hdDirectoryInWhichToSearch.GetDirectories("*" + partialName + "*.*");

    foreach (FileInfo foundFile in filesInDir)
    {
        string fullName = foundFile.FullName;
        richtxt_results.Text += fullName + "\n";

    }

    foreach (DirectoryInfo foundDir in dirsInDir)
    {
        string fullName = foundDir.FullName;
        richtxt_results.Text += fullName + "\n";
    }
}

1 个答案:

答案 0 :(得分:5)

    public static void CountFiles(string path)
    {
        int xFileCount = 0;
        int yFileCount = 0;
        int zFileCount = 0;

        var files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories);

        foreach(string file in files)
        {
            string folder = new DirectoryInfo(Path.GetDirectoryName(file)).Name;
            if (folder == "FOLDER_X")
                xFileCount++;
            if (folder == "FOLDER_Y")
                yFileCount++;
            if (folder == "FOLDER_Z")
                zFileCount++;
        }

        Console.WriteLine("X Files : {0}", xFileCount);
        Console.WriteLine("Y Files : {0}", yFileCount);
        Console.WriteLine("Z Files : {0}", zFileCount);
    }

几点提示:

  • 如果要搜索特定类型的文件(例如,只有文本文件),则可以将搜索模式传递​​给Directory.GetFiles(),例如" .txt&#34 ;而不是" 。*"。
  • 如果您想使其更通用,而不是仅仅硬编码您的文件夹名称,您可以将其作为参数传递。

所以我真的会使用这个函数,并用你想要的任何文件夹名称来调用它:

    public static int CountFiles(string path, string folderToSearch)
    {
        int fileCount = 0;

        var files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories);

        foreach (string file in files)
        {
            string folder = new DirectoryInfo(Path.GetDirectoryName(file)).Name;
            if (folder == folderToSearch)
                fileCount++;
        }

        return fileCount;
    }

然后这样称呼它:

    static void Main(string[] args)
    {
        int xFiles = CountFiles("path goes here", "FOLDER_X");
        int yFiles = CountFiles("path goes here", "FOLDER_Y");
        int zFiles = CountFiles("path goes here", "FOLDER_Z");
    }

编辑:对如何获得直接目录名称进行了一些小改动。

EDIT2:编辑以纳入@MickyD的建议。

EDIT3:这会通过你最近的要求。

    public static int CountFiles2(string path, string folderToSearch)
    {
        int fileCount = 0;
        var dirs = Directory.EnumerateDirectories(path, folderToSearch, SearchOption.AllDirectories).ToList();

        foreach (string dir in dirs)
        {
            var files = Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories);
            if (files != null)
                fileCount += files.Count();
        }

        return fileCount;
    }