Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to iterate folders and subfolders

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?

like image 729
Rod Avatar asked Mar 03 '11 13:03

Rod


People also ask

How do I view all subfolders at once?

There are a number of ways to display a folder in File Explorer: Click on a folder if it's listed in the Navigation pane. Click on a folder in the Address bar to display its subfolders. Double-click on a folder in the file and folder listing to display any subfolders.

How do I loop through a folder?

To loop through a directory, and then print the name of the file, execute the following command: for FILE in *; do echo $FILE; done.


2 Answers

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.

like image 85
Chris Dunaway Avatar answered Sep 29 '22 10:09

Chris Dunaway


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);            } } 
like image 41
Lloyd Avatar answered Sep 29 '22 11:09

Lloyd