0

I have a path structure that looks like this:

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 

I basically want the user to input a path to the main folder, then iterate through each sub_folder. Then output the total number of files in ALL Folder_Y’s from all the sub folders. And the same for Folder_X, Folder_Z.

I’ve tried several methods unsuccessfully.

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 Answer 1

4
 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); } 

Few tips:

  • If you want to search for a specific type of files (say, for example, only text files) then you can pass search pattern to Directory.GetFiles(), such as ".txt" instead of ".*".
  • If you want to make this more generic, instead of just hardcoding your folder names you could pass that as a parameter.

So I would really use this function, and call it with whatever folder name you want:

 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; } 

Then call it like so:

 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"); } 

EDIT: Made a small change to how you get the immediate directory name.

EDIT2: Edited to incorporate the suggestion by @MickyD.

EDIT3: This would pass your most recent requirement.

 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; } 
Sign up to request clarification or add additional context in comments.

11 Comments

Note - you should use EnumerateFiles when dealing with large number of files particularly with child folders because it returns an IEnumerable<> rather than an array.
I did not know that and seems like a useful piece of information to know. Thanks!
Thank you for taking the time to answer. However, I am getting an error at string folder = new DirectoryInfo(Path.GetDirectoryName(file)).Name; It is somehow searching for the folder inside the directory that my C# project is on instead of the path. Even though I am calling the function with the appropriate path.
@AR.Torres did you pass the correct path in place of "path goes here"? I suggest you use the second method I gave which I tested. Please double check. What's the error you get? It's highly questionable because if you go inside the foreach loop then that means you already have found files wherever you're looking.
Yes, there was an issue with the path so that is now solved. But I forgot to mentioned the folders there are other other folders and I want to get the count of those, so right now your code is outputting 0. So lets say FOLDER_X has 20 folders and inside each X number files I am trying to otput the combined X number of each folder named FOLDER_X within the path if that makes sense. So basically from the path, go to only one child down on each folder search for the name, if it matches then count ALL files inside that folder and accumulate it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.