Advertisement
C0BRA

FS Delta

Dec 13th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using System.IO;
  7.  
  8. namespace Sizer
  9. {
  10.     class Program
  11.     {
  12.         static void GetFiles(string dir, ref List<string> output, ref Dictionary<string, long> Sizes)
  13.         {
  14.             //Console.WriteLine("entering {0}", dir);
  15.             string[] dirs;
  16.             string[] files;          
  17.            
  18.             try
  19.             {
  20.  
  21.                 files = Directory.GetFiles(dir, "*.*", SearchOption.TopDirectoryOnly);
  22.                 long ii = 0;
  23.  
  24.                 foreach (string file in files)
  25.                 {
  26.                     try
  27.                     {
  28.                         Console.Write("\rfile {0} of {1}", ii, files.Length);
  29.                         ii++;
  30.  
  31.                         FileInfo info = new FileInfo(file);
  32.  
  33.                         // If you want to just show DIR sizes, and not file sizes, then remove the /* and */
  34.                         string current = /*Path.GetDirectoryName*/(file);
  35.  
  36.                         while (true)
  37.                         {
  38.                             long o;
  39.                             if (!Sizes.TryGetValue(current, out o))
  40.                                 o = 0;
  41.  
  42.                             Sizes[current] = o + info.Length;
  43.  
  44.                             DirectoryInfo dirinfo = Directory.GetParent(current);
  45.                             if (dirinfo == null)
  46.                                 break;
  47.  
  48.                             current = dirinfo.FullName;
  49.                         }
  50.                     }
  51.                     catch { }
  52.                 }
  53.             }
  54.             catch { }
  55.  
  56.             Console.Write('\r');
  57.             for (int x = 0; x < Console.BufferWidth - 1; x++)
  58.                 Console.Write(' ');
  59.             Console.Write('\r');
  60.            
  61.             try
  62.             {
  63.                 dirs = Directory.GetDirectories(dir, "*", SearchOption.TopDirectoryOnly);
  64.             }
  65.             catch { return; }
  66.  
  67.             double i = 0;
  68.             foreach (string dir2 in dirs)
  69.             {
  70.                 string dirname;
  71.                 try
  72.                 {
  73.                     dirname = Path.GetFileName(/*Path.GetDirectoryName*/(Path.GetDirectoryName(dir2)));
  74.                 }
  75.                 catch { continue; }
  76.  
  77.                 if (dirname == "") dirname = Path.GetPathRoot(dir2);
  78.  
  79.                 if (dirname.Length > (Console.BufferWidth - "100% ".Length))
  80.                     dirname = dirname.Substring(0, Console.BufferWidth - "100% ...".Length) + "...";
  81.  
  82.                 Console.WriteLine("{0:000}% {1}", Math.Round(i / (double)dirs.Length * 100.0), dirname);
  83.                 i++;
  84.                 GetFiles(dir2, ref output, ref Sizes);
  85.                 Console.SetCursorPosition(0, Console.CursorTop - 1);
  86.                 Console.CursorLeft = 0;
  87.                 for (int x = 0; x < Console.WindowWidth; x++)
  88.                     Console.Write(" ");
  89.                 Console.SetCursorPosition(0, Console.CursorTop - 1);
  90.             }
  91.            
  92.         }
  93.  
  94.         static void Compare(string a, string b)
  95.         {
  96.             string outname = string.Format("delta between {0} and {1}.txt", Path.GetFileName(a.Replace(".txt", "")), Path.GetFileName(b.Replace(".txt", "")));
  97.  
  98.             StreamReader ra = new StreamReader(a);
  99.             StreamReader rb = new StreamReader(b);
  100.             // b = new, a = old
  101.  
  102.             Dictionary<string, long> HashMap_A = new Dictionary<string, long>();
  103.             Dictionary<string, long> HashMap_B = new Dictionary<string, long>();
  104.  
  105.             ///// B
  106.  
  107.             string line = ra.ReadLine();
  108.  
  109.             Console.WriteLine("loading {0}...", a);
  110.  
  111.             while (line != null)
  112.             {
  113.                 string[] split = line.Split(" ".ToCharArray(), 2);
  114.  
  115.                 long size = long.Parse(split[0]);
  116.                 HashMap_A[split[1]] = size;
  117.  
  118.                 line = ra.ReadLine();
  119.             }
  120.  
  121.             Console.WriteLine("loading {0}...", b);
  122.  
  123.             ///// A
  124.  
  125.             line = rb.ReadLine();
  126.  
  127.             while (line != null)
  128.             {
  129.                 string[] split = line.Split(" ".ToCharArray(), 2);
  130.  
  131.                 long size = long.Parse(split[0]);
  132.                 HashMap_B[split[1]] = size;
  133.  
  134.                 line = rb.ReadLine();
  135.             }
  136.  
  137.             ///// DELTA
  138.  
  139.             Console.WriteLine("finding delta...");
  140.  
  141.             Console.Write("total difference: 0B");
  142.             long total_diff = 0;
  143.  
  144.             StreamWriter w = new StreamWriter(outname);
  145.  
  146.             foreach (KeyValuePair<string, long> kv in HashMap_B)
  147.             {
  148.                 long old;
  149.                 if (!HashMap_A.TryGetValue(kv.Key, out old))
  150.                     old = 0;
  151.  
  152.                 long difference = kv.Value - old;
  153.  
  154.                 if (difference == 0)
  155.                     continue;
  156.  
  157.                 total_diff += difference;
  158.                 Console.Write('\r');
  159.                 for (int i = 0; i < Console.BufferWidth - 1; i++)
  160.                     Console.Write(' ');
  161.                 Console.Write('\r');
  162.  
  163.                 Console.Write("total difference: {0}", MakeNiceSize(total_diff));
  164.  
  165.                 string diff = MakeNiceSize(difference);
  166.                 w.WriteLine("{0} \t\t\t{1}", diff, kv.Key);
  167.             }
  168.  
  169.             // Deleted files
  170.             foreach (KeyValuePair<string, long> kv in HashMap_A)
  171.             {
  172.                 long newsze;
  173.                 if (!HashMap_B.TryGetValue(kv.Key, out newsze))
  174.                 {
  175.                     {
  176.                         total_diff += -kv.Value;
  177.                         Console.Write('\r');
  178.                         for (int i = 0; i < Console.BufferWidth - 1; i++)
  179.                             Console.Write(' ');
  180.                         Console.Write('\r');
  181.  
  182.                         Console.Write("total difference: {0}", MakeNiceSize(total_diff));
  183.                     }
  184.  
  185.                     string diff = MakeNiceSize(-kv.Value);
  186.                     w.WriteLine("{0}* \t\t\t{1}", diff, kv.Key);
  187.                 }
  188.             }
  189.  
  190.             Console.WriteLine();
  191.  
  192.             w.Flush();
  193.             w.Close();
  194.         }
  195.  
  196.         static char[] NiceBytes = new char[] {' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
  197.  
  198.         static string MakeNiceSize(long x)
  199.         {
  200.             long original = x;
  201.             double y = (double)x;
  202.  
  203.             int madesmaller = 0;
  204.  
  205.             while (Math.Abs(y) >= 1024)
  206.             {
  207.                 madesmaller++;
  208.                 y = y / 1024.0;
  209.             }
  210.  
  211.             if (madesmaller == 0 || madesmaller >= NiceBytes.Length /* woah, that's one big file! */)
  212.                 return string.Format("{0}B", original);
  213.  
  214.             return string.Format("{0:#.##}{1}iB", y, NiceBytes[madesmaller]);
  215.         }
  216.  
  217.         static void Main(string[] args)
  218.         {
  219.             if (args.Length == 4 && args[1] == "--compare")
  220.             {
  221.                 Compare(args[2], args[3]);
  222.                 return;
  223.             }
  224.  
  225.             if (args.Length == 3 && args[0] == "--compare")
  226.             {
  227.                 Compare(args[1], args[2]);
  228.                 return;
  229.             }
  230.  
  231.             string root = @"C:\";
  232.  
  233.             if (args.Length >= 2 && args[0] == "--path")
  234.                 root = args[1];
  235.             if (args.Length >= 3 && args[1] == "--path")
  236.                 root = args[2];
  237.  
  238.             Console.WriteLine("getting files... ({0})", root);
  239.  
  240.             List<string> files = new List<string>();
  241.             Dictionary<string, long> Sizes = new Dictionary<string, long>();
  242.             GetFiles(root, ref files, ref Sizes);
  243.  
  244.  
  245.  
  246.             Console.WriteLine("dumping to file");
  247.  
  248.             string of = DateTime.UtcNow.ToString("dd MMM HH-mm") + ".txt";
  249.  
  250.             StreamWriter w = new StreamWriter(of);
  251.            
  252.             double i = 0;
  253.             foreach (KeyValuePair<string, long> kv in Sizes)
  254.             {
  255.                 w.WriteLine("{0} {1}", kv.Value, kv.Key);
  256.                 i++;
  257.                 Console.Write("\rdumped file {0} of {1} ({2:000.00}%)", i, Sizes.Count, i / (double)Sizes.Count * 100.0);
  258.             }
  259.  
  260.             Console.WriteLine();
  261.  
  262.             w.Flush();
  263.             w.Close();
  264.         }
  265.  
  266.     }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement