-1

So this seems to me to be a simple looping issue it's just that I keep confusing myself over the logic.

So I want to count all the files within a folder, and then all the folders within that folder, I want to count the files that are in there too.

Which mean I have to loop through to check wether there is a folder and then check it until there are no more folders. But I can't write the algoritm because I keep confusing myself.

I'm pretty sure there is a standard algorithm for something like this but I can't remember the name.

This is what I have so far:

var rootDir = Directory.GetDirectories(@"C:\");

foreach (var dir in rootDir)
{
    if (Directory.GetDirectories(dir).Length > 0)
    {

    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mark Denom
  • 987
  • 1
  • 8
  • 24
  • Use [Recursion](https://en.wikipedia.org/wiki/Recursion). Or, as an alternative, use [iteration](https://stackoverflow.com/q/159590/107625). – Uwe Keim Mar 26 '19 at 05:25
  • 4
    This has been asked 4023432 times + yours today, there is also an issue with permissions that you will probably run into which will cause problems, so instead of getting an answer that will lead you into another question, i would suggest digging around on google to see all the best answers to this question – TheGeneral Mar 26 '19 at 05:27
  • **Note** : all the formal answers so far will fail if there is a permissions issue, and you may get a stack overflow exception if you have a highly nested folder structure and you use recursion – TheGeneral Mar 26 '19 at 05:31

3 Answers3

2

Do I understand right, you need to count only files in folder and all subfolders? Directory.GetFiles has option for review all subfolders. Try this

Directory.GetFiles(WorkingDir, "*", SearchOption.AllDirectories);
Lana
  • 1,024
  • 1
  • 7
  • 14
  • `int result = Directory.EnumerateFiles(WorkingDir, "*", SearchOption.AllDirectories).Count();` can be a better implementation: we don't want a (large) array be created but count the items – Dmitry Bychenko Mar 26 '19 at 08:15
  • 1
    That's great too! I actually needed the path for each file so that first worked, I figured there would be a parameter like `SearchOption.AllDirectories` Thank you! :-) – Mark Denom Mar 26 '19 at 08:18
0

use GetFiles

Directory.GetFiles

// Process all files in the directory passed in, recurse on any directories 
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory) 
{
    // Process the list of files found in the directory.
    string [] fileEntries = Directory.GetFiles(targetDirectory);
    foreach(string fileName in fileEntries)
        ProcessFile(fileName);

    // Recurse into subdirectories of this directory.
    string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
    foreach(string subdirectory in subdirectoryEntries)
        ProcessDirectory(subdirectory);
}

// Insert logic for processing found files here.
public static void ProcessFile(string path) 
{
    Console.WriteLine("Processed file '{0}'.", path);       
}
Jophy job
  • 1,924
  • 2
  • 20
  • 38
0

here is a solution to count all files files and number of file per dir: i am using a dictionary to stock all datas

class Program
{
    public static Dictionary<string, int> dico = new Dictionary<string, int>();

    public static void CountFiles(string nameDirectory)
    {
        int nbrfiles = Directory.GetFiles(nameDirectory).Length;
        dico[targetDirectory] = nbrfiles;

        string[] subdirectories = Directory.GetDirectories(nameDirectory);
        foreach (string subdir in subdirectories)
            CountFiles(subdir);
    }
    static void Main(string[] args)
    {
        string tdir = "e:\\example";
        CountFiles(tdir);
        var totalfiles = dico.Sum(x => x.Value);
        Console.WriteLine($"Directory {tdir} contains {totalfiles} files");
        foreach (var item in dico)
        {
            Console.WriteLine($"Directory {item.Key} has {item.Value} file(s)");
        }
    }
} 
Frenchy
  • 16,386
  • 3
  • 16
  • 39