Advertisement
dimipan80

Directory Traversal

May 22nd, 2015
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. /* Traverse a given directory for all files with the given extension. Search through the first level of the directory only and write information about each found file in report.txt.
  2.  * The files should be grouped by their extension. Extensions should be ordered by the count of their files (from most to least). If two extensions have equal number of files, order them by name.
  3.  * Files under an extension should be ordered by their size.
  4.  * report.txt should be saved on the Desktop. Ensure the desktop path is always valid, regardless of the user. */
  5.  
  6. namespace _07.Directory_Traversal
  7. {
  8.     using System;
  9.     using System.Collections.Generic;
  10.     using System.IO;
  11.     using System.Linq;
  12.  
  13.     class DirectoryTraversal
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Console.WriteLine("Enter (relative) path to your Directory, like this - '../../' :");
  18.             string directoryPath = Console.ReadLine();
  19.             if (!Directory.Exists(directoryPath))
  20.             {
  21.                 Console.WriteLine("This path is not valid Directory!!!");
  22.                 return;
  23.             }
  24.  
  25.             FileInfo[] insideFiles = Directory.GetFiles(directoryPath)
  26.                 .Select(file => new FileInfo(file)).ToArray();
  27.  
  28.             Dictionary<string, List<FileInfo>> fileGroups = new Dictionary<string, List<FileInfo>>();
  29.             FillGroups(insideFiles, fileGroups);
  30.  
  31.             var results = fileGroups
  32.                 .OrderByDescending(group => group.Value.Count)
  33.                 .ThenBy(group => group.Key);
  34.  
  35.             PrintResults(results);
  36.         }
  37.  
  38.         private static void FillGroups(FileInfo[] files, Dictionary<string, List<FileInfo>> fileTypes)
  39.         {
  40.             foreach (FileInfo file in files)
  41.             {
  42.                 string fileExtension = file.Extension;
  43.  
  44.                 if (!fileTypes.ContainsKey(fileExtension))
  45.                 {
  46.                     fileTypes[fileExtension] = new List<FileInfo>();
  47.                 }
  48.  
  49.                 fileTypes[fileExtension].Add(file);
  50.             }
  51.         }
  52.  
  53.         private static void PrintResults(IOrderedEnumerable<KeyValuePair<string, List<FileInfo>>> groups)
  54.         {
  55.             string outputFile = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\report.txt";
  56.             using (var writer = new StreamWriter(outputFile))
  57.             {
  58.                 foreach (var keyValuePair in groups)
  59.                 {
  60.                     writer.WriteLine(keyValuePair.Key);
  61.                     var typeFiles = keyValuePair.Value
  62.                         .OrderBy(file => file.Length);
  63.  
  64.                     foreach (var fileInfo in typeFiles)
  65.                     {
  66.                         double fileSize = Math.Round(fileInfo.Length / 1024.0, 3);
  67.                         writer.WriteLine("--{0} - {1}kb", fileInfo.Name, fileSize);
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement