Advertisement
bomman

Grouping files

Oct 4th, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.        
  10.         string directory = Console.ReadLine();
  11.         string[] allFiles = Directory.GetFiles(directory);
  12.         DirectoryInfo name = new DirectoryInfo(directory);
  13.         FileInfo[] whole = name.GetFiles();
  14.         IOrderedEnumerable<IGrouping<string, FileInfo>> queryGroupByExt =
  15.              from file in whole
  16.              group file by file.Extension.ToLower() into fileGroup
  17.              orderby  whole.Length descending
  18.              select fileGroup;
  19.  
  20.         Grouping(queryGroupByExt);
  21.     }
  22.     static void Grouping(System.Linq.IOrderedEnumerable<System.Linq.IGrouping<string, System.IO.FileInfo>> files)
  23.     {
  24.         using (StreamWriter destination = new StreamWriter(@"C:\Users\asus\Desktop\text.txt"))
  25.         {
  26.             foreach (var n in files)
  27.             {
  28.                 destination.WriteLine(n.Key == String.Empty ? "[none]" : n.Key);
  29.                 foreach (var item in n)
  30.                 {
  31.                     destination.WriteLine("--{0} - {1}kb", item, item.Length);
  32.                     Console.WriteLine(item);
  33.                 }
  34.  
  35.             }
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement