/ Published in: C#
count the folders and subfolders of a directory
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// 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) { System.Console.WriteLine("parameter count = {0}", args.Length); if (Directory.Exists(args[0])) { // This path is a directory Console.WriteLine("Il y a {0} repertoires dans {1}.", ProcessDirectory(args[0]), args[0]); } else { Console.WriteLine("{0} is not a valid directory.", args[0]); } } // Process all directories // that are found, and process the files they contain. public static int ProcessDirectory(string targetDirectory) { int resultat = 0; // Recurse into subdirectories of this directory. string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory); foreach(string subdirectory in subdirectoryEntries) // pour chaque sous répertoire resultat = resultat + 1 + ProcessDirectory(subdirectory); // on compte le répertoire actuel au quel on ajoute le nombre de sous-dossiers return resultat; } }