Posted By

martinbrait on 08/23/19


Tagged

count files directory


Versions (?)

[c#] [directory] compter les dossiers et les sous-dossiers d'un répertoire


 / Published in: C#
 

count the folders and subfolders of a directory

  1. // For Directory.GetFiles and Directory.GetDirectories
  2. // For File.Exists, Directory.Exists
  3. using System;
  4. using System.IO;
  5. using System.Collections;
  6.  
  7. public class RecursiveFileProcessor
  8. {
  9. public static void Main(string[] args)
  10. {
  11. System.Console.WriteLine("parameter count = {0}", args.Length);
  12. if (Directory.Exists(args[0]))
  13. {
  14. // This path is a directory
  15. Console.WriteLine("Il y a {0} repertoires dans {1}.", ProcessDirectory(args[0]), args[0]);
  16. }
  17. else
  18. {
  19. Console.WriteLine("{0} is not a valid directory.", args[0]);
  20. }
  21. }
  22.  
  23.  
  24. // Process all directories
  25. // that are found, and process the files they contain.
  26. public static int ProcessDirectory(string targetDirectory)
  27. {
  28. int resultat = 0;
  29. // Recurse into subdirectories of this directory.
  30. string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
  31. foreach(string subdirectory in subdirectoryEntries) // pour chaque sous répertoire
  32. resultat = resultat + 1 + ProcessDirectory(subdirectory); // on compte le répertoire actuel au quel on ajoute le nombre de sous-dossiers
  33.  
  34. return resultat;
  35. }
  36. }

Report this snippet  

You need to login to post a comment.