46

What's the best way to iterate folders and subfolders to get file size, total number of files, and total size of folder in each folder starting at a specified location?

bluish
  • 26,356
  • 27
  • 122
  • 180
Rod
  • 14,529
  • 31
  • 118
  • 230
  • Just two words: foreach and recursion.. – Andrew Orsich Mar 03 '11 at 13:38
  • 1
    @Bugai13 - That's a good suggestion for CS homework, but the .Net framework already includes that functionality. As an aside, moving away from iterating over collections/enumerables and towards querying collections/enumerables, or even giving the collection/enumerable the work to do is the correct way to solve that problem in a modern context. – Ritch Melton Mar 03 '11 at 13:43
  • 1
    please fix the title typo: i**n**terate and capitalize the first letter. – HuBeZa Mar 03 '11 at 13:52
  • @Pekka at first I wanted to explore different ways but I guess I'll stick with c# – Rod Mar 03 '11 at 14:20

6 Answers6

59

If you're using .NET 4, you may wish to use the System.IO.DirectoryInfo.EnumerateDirectories and System.IO.DirectoryInfo.EnumerateFiles methods. If you use the Directory.GetFiles method as other posts have recommended, the method call will not return until it has retrieved ALL the entries. This could take a long time if you are using recursion.

From the documentation:

The EnumerateFilesand GetFiles methods differ as follows:

  • When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.
  • When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array.

Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
  • 2
    Had to skip dozen of examples of using GetDirectory() to find this one. With >50k directories it was too slow. Thanks for the rare example that saved my day. – krowe2 Aug 14 '18 at 20:05
47

Use Directory.GetFiles(). The bottom of that page includes an example that's fully recursive.

Note: Use Chris Dunaway's answer below for a more modern approach when using .NET 4 and above.

// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor 
{
    public static void Main(string[] args) 
    {
        foreach(string path in args) 
        {
            if(File.Exists(path)) 
            {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) 
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else 
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }

    // 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);       
    }
}
Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • 1
    One caveat: Directory.GetFiles() can be *extremely* slow with directories containing large numbers of files (10's to 100's of thousands). In these cases the fastest way I've found so far is actually to start a process to run a `dir` command and redirect the output and parse it (or pipe into a file and read that). Granted, I don't consider this unless I expect 50,000+ files in a single directory. – quentin-starin May 13 '11 at 20:56
  • Actually it may be quicker to pinvoke `FindFirstFile` etc instead of using `dir` although I'm sure .NET thunks down to this anyhow. – Lloyd Jan 15 '13 at 00:52
8

To iterate through all directories sub folders and files, no matter how much sub folder and files are.

string [] filenames;
 fname = Directory.GetFiles(jak, "*.*", SearchOption.AllDirectories).Select(x => Path.GetFileName(x)).ToArray();

then from array you can get what you want via a loop or as you want.

safi
  • 3,636
  • 8
  • 24
  • 37
  • 1
    This will infinite loop if there's a loop in your folder structure: see http://msdn.microsoft.com/en-us/library/ms143448.aspx – Anthony Wieser Oct 02 '13 at 07:30
5

Here's an example using Peter's suggestion above and recursion.

using System;
using System.IO;

namespace FileSystemUtils
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderPath = "C:\\docs";

            DirectoryInfo startDir = new DirectoryInfo(folderPath);

            RecurseFileStructure recurseFileStructure = new RecurseFileStructure();
            recurseFileStructure.TraverseDirectory(startDir);
        }

        public class RecurseFileStructure
        {
            public void TraverseDirectory(DirectoryInfo directoryInfo)
            {
                var subdirectories = directoryInfo.EnumerateDirectories();

                foreach (var subdirectory in subdirectories)
                {
                    TraverseDirectory(subdirectory);
                }

                var files = directoryInfo.EnumerateFiles();

                foreach (var file in files)
                {
                    HandleFile(file);
                }
            }

            void HandleFile(FileInfo file)
            {
                Console.WriteLine("{0}", file.Name);
            }
        }
    }
}
S4NT14G0
  • 164
  • 2
  • 6
1

To iterate through files and folders you would normally use the DirectoryInfo and FileInfo types. The FileInfo type has a Length property that returns the file size in bytes.

I think you must write your own code to iterate through the files and calculate the total file size, but it should be a quite simple recursive function.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76
1

Note that you will need to perform validation checks.

string[] fileNames = Directory.GetFiles("c:\\", "*.*", SearchOption.AllDirectories);
int fileCount = fileNames.Count();
long fileSize = fileNames.Select(file => new FileInfo(file).Length).Sum(); // in bytes
HuBeZa
  • 4,715
  • 3
  • 36
  • 58