Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace Sizer
- {
- class Program
- {
- static void GetFiles(string dir, ref List<string> output, ref Dictionary<string, long> Sizes)
- {
- //Console.WriteLine("entering {0}", dir);
- string[] dirs;
- string[] files;
- try
- {
- files = Directory.GetFiles(dir, "*.*", SearchOption.TopDirectoryOnly);
- long ii = 0;
- foreach (string file in files)
- {
- try
- {
- Console.Write("\rfile {0} of {1}", ii, files.Length);
- ii++;
- FileInfo info = new FileInfo(file);
- // If you want to just show DIR sizes, and not file sizes, then remove the /* and */
- string current = /*Path.GetDirectoryName*/(file);
- while (true)
- {
- long o;
- if (!Sizes.TryGetValue(current, out o))
- o = 0;
- Sizes[current] = o + info.Length;
- DirectoryInfo dirinfo = Directory.GetParent(current);
- if (dirinfo == null)
- break;
- current = dirinfo.FullName;
- }
- }
- catch { }
- }
- }
- catch { }
- Console.Write('\r');
- for (int x = 0; x < Console.BufferWidth - 1; x++)
- Console.Write(' ');
- Console.Write('\r');
- try
- {
- dirs = Directory.GetDirectories(dir, "*", SearchOption.TopDirectoryOnly);
- }
- catch { return; }
- double i = 0;
- foreach (string dir2 in dirs)
- {
- string dirname;
- try
- {
- dirname = Path.GetFileName(/*Path.GetDirectoryName*/(Path.GetDirectoryName(dir2)));
- }
- catch { continue; }
- if (dirname == "") dirname = Path.GetPathRoot(dir2);
- if (dirname.Length > (Console.BufferWidth - "100% ".Length))
- dirname = dirname.Substring(0, Console.BufferWidth - "100% ...".Length) + "...";
- Console.WriteLine("{0:000}% {1}", Math.Round(i / (double)dirs.Length * 100.0), dirname);
- i++;
- GetFiles(dir2, ref output, ref Sizes);
- Console.SetCursorPosition(0, Console.CursorTop - 1);
- Console.CursorLeft = 0;
- for (int x = 0; x < Console.WindowWidth; x++)
- Console.Write(" ");
- Console.SetCursorPosition(0, Console.CursorTop - 1);
- }
- }
- static void Compare(string a, string b)
- {
- string outname = string.Format("delta between {0} and {1}.txt", Path.GetFileName(a.Replace(".txt", "")), Path.GetFileName(b.Replace(".txt", "")));
- StreamReader ra = new StreamReader(a);
- StreamReader rb = new StreamReader(b);
- // b = new, a = old
- Dictionary<string, long> HashMap_A = new Dictionary<string, long>();
- Dictionary<string, long> HashMap_B = new Dictionary<string, long>();
- ///// B
- string line = ra.ReadLine();
- Console.WriteLine("loading {0}...", a);
- while (line != null)
- {
- string[] split = line.Split(" ".ToCharArray(), 2);
- long size = long.Parse(split[0]);
- HashMap_A[split[1]] = size;
- line = ra.ReadLine();
- }
- Console.WriteLine("loading {0}...", b);
- ///// A
- line = rb.ReadLine();
- while (line != null)
- {
- string[] split = line.Split(" ".ToCharArray(), 2);
- long size = long.Parse(split[0]);
- HashMap_B[split[1]] = size;
- line = rb.ReadLine();
- }
- ///// DELTA
- Console.WriteLine("finding delta...");
- Console.Write("total difference: 0B");
- long total_diff = 0;
- StreamWriter w = new StreamWriter(outname);
- foreach (KeyValuePair<string, long> kv in HashMap_B)
- {
- long old;
- if (!HashMap_A.TryGetValue(kv.Key, out old))
- old = 0;
- long difference = kv.Value - old;
- if (difference == 0)
- continue;
- total_diff += difference;
- Console.Write('\r');
- for (int i = 0; i < Console.BufferWidth - 1; i++)
- Console.Write(' ');
- Console.Write('\r');
- Console.Write("total difference: {0}", MakeNiceSize(total_diff));
- string diff = MakeNiceSize(difference);
- w.WriteLine("{0} \t\t\t{1}", diff, kv.Key);
- }
- // Deleted files
- foreach (KeyValuePair<string, long> kv in HashMap_A)
- {
- long newsze;
- if (!HashMap_B.TryGetValue(kv.Key, out newsze))
- {
- {
- total_diff += -kv.Value;
- Console.Write('\r');
- for (int i = 0; i < Console.BufferWidth - 1; i++)
- Console.Write(' ');
- Console.Write('\r');
- Console.Write("total difference: {0}", MakeNiceSize(total_diff));
- }
- string diff = MakeNiceSize(-kv.Value);
- w.WriteLine("{0}* \t\t\t{1}", diff, kv.Key);
- }
- }
- Console.WriteLine();
- w.Flush();
- w.Close();
- }
- static char[] NiceBytes = new char[] {' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
- static string MakeNiceSize(long x)
- {
- long original = x;
- double y = (double)x;
- int madesmaller = 0;
- while (Math.Abs(y) >= 1024)
- {
- madesmaller++;
- y = y / 1024.0;
- }
- if (madesmaller == 0 || madesmaller >= NiceBytes.Length /* woah, that's one big file! */)
- return string.Format("{0}B", original);
- return string.Format("{0:#.##}{1}iB", y, NiceBytes[madesmaller]);
- }
- static void Main(string[] args)
- {
- if (args.Length == 4 && args[1] == "--compare")
- {
- Compare(args[2], args[3]);
- return;
- }
- if (args.Length == 3 && args[0] == "--compare")
- {
- Compare(args[1], args[2]);
- return;
- }
- string root = @"C:\";
- if (args.Length >= 2 && args[0] == "--path")
- root = args[1];
- if (args.Length >= 3 && args[1] == "--path")
- root = args[2];
- Console.WriteLine("getting files... ({0})", root);
- List<string> files = new List<string>();
- Dictionary<string, long> Sizes = new Dictionary<string, long>();
- GetFiles(root, ref files, ref Sizes);
- Console.WriteLine("dumping to file");
- string of = DateTime.UtcNow.ToString("dd MMM HH-mm") + ".txt";
- StreamWriter w = new StreamWriter(of);
- double i = 0;
- foreach (KeyValuePair<string, long> kv in Sizes)
- {
- w.WriteLine("{0} {1}", kv.Value, kv.Key);
- i++;
- Console.Write("\rdumped file {0} of {1} ({2:000.00}%)", i, Sizes.Count, i / (double)Sizes.Count * 100.0);
- }
- Console.WriteLine();
- w.Flush();
- w.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement