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.Threading.Tasks;
- using System.IO;
- class DirectorySize {
- //Member variables
- //Is our directory we're technically searching in valid, and moreso can we actually do anything.
- private bool Valid = true;
- //The original directory we're looking in.
- private string baseDir;
- //Our dictionary of each directory in the baseDir with the size as a long
- private Dictionary<string, long> directorys;
- //Constructors
- //direct is what directory we're going to have as our baseDir
- //subDir is a bool stating if we want to individualize each subdirectory in baseDir so each gets it's own size. True for the example
- public DirectorySize(string direct, bool subDir) {
- //If our directory actually exists, just gotta make sure
- if (Directory.Exists(direct)) {
- //Set baseDir to direct, and initialize directorys. Could of actually done that above, but meh.
- baseDir = direct;
- directorys = new Dictionary<string, long>();
- //If we want the subdirectory sizes
- if (subDir) {
- //Just add them into the dictionary with a "size" of 0
- foreach (string directs in Directory.GetDirectories(direct)) {
- directorys[directs] = 0;
- }
- } else {
- //Just add our base directory into the dictionary with size of 0
- directorys[direct] = 0;
- }
- } else {
- //We're not a valid setup, we would need to be remade with a different starting directory
- Valid = false;
- }
- }
- //Default values for direct and subDir are C:\ and true
- public DirectorySize(string direct) : this(direct, true) {
- }
- public DirectorySize() : this(@"C:\") {
- }
- //String reverse function, I need it for adding the comma's
- private string Reverse(string s) {
- //Turn s to an array, reverse the array, then return the reversed array as a string
- char[] c = s.ToCharArray();
- Array.Reverse(c);
- return new string(c);
- }
- //Recursive function for getting the size of a given directory and any directory's in them as a long
- //Directory is the current directory, stringList is our messages list so what directory's we can't access.
- private long recurFindSize(DirectoryInfo directory, List<string> stringList) {
- //Trycatch block for if we can't access the directory.
- try {
- //Our long value to return, starts at 0 so I can add to it
- //Originally I had it as an int when I started this, turns out that file.Length is a long and c# don't like long to int conversion
- long returnInt = 0;
- //Loop through each fileInfo file inside the directory's files
- foreach (var file in directory.EnumerateFiles()) {
- //Add the length
- returnInt += file.Length;
- }
- //Loop through each DirectoryInfo direct in directory
- foreach (var direct in directory.EnumerateDirectories()) {
- //Add return value from calling recurrsive function to the long we need to return
- returnInt += recurFindSize(direct, stringList);
- }
- //Return our long
- return returnInt;
- } catch (Exception) {
- //If we had an exception, add our directory path to the error list, and return 0
- stringList.Add(directory.FullName);
- return 0;
- }
- }
- //Our public function for finding the size of a directory, makes it easy to use really based off directory's name
- public long findSize(string fileName, List<string> stringList) {
- return recurFindSize(new DirectoryInfo(fileName), stringList);
- }
- //Our printing function
- public void printSizes() {
- //If we're a valid class
- if (Valid) {
- //Our error list, and a copy of the directorys dictionary
- //Need the copy because we cannot edit the original while enumerating through it
- List<string> errorList = new List<string>();
- Dictionary<string, long> s = new Dictionary<string, long>(directorys);
- //Enumerate through the copied dictionary
- foreach (var entry in s) {
- //Set the sizes in our original dictionary and pass it the directory name, and our errorList
- directorys[entry.Key] = findSize(entry.Key, errorList);
- }
- //If we had any directorys we couldn't access, print the banner and each directory
- if (errorList.Count > 0) {
- Console.WriteLine("=========================================");
- Console.WriteLine("Messages");
- Console.WriteLine("=========================================");
- Console.WriteLine("");
- foreach (string fileNames in errorList) {
- Console.WriteLine("Could not read directory: [{0}]", fileNames);
- }
- Console.WriteLine("");
- }
- //Use linq to order our KeyValuePairs in the dictionary based on the size
- var ordered = from p in directorys orderby p.Value descending select p;
- //Print the banner
- Console.WriteLine("=========================================");
- Console.WriteLine("Directory Sizes");
- Console.WriteLine("=========================================");
- Console.WriteLine("");
- //Print the base directory
- Console.WriteLine("Base Directory: [{0}]\n", baseDir);
- //Our padding size, starts at 0, and will be the size of the first/largest (they're the same) value in our KeyValuePair Array
- var padSize = 0;
- //Loop through our ordered array
- foreach (var current in ordered) {
- //Adding commas
- //Reverse string, and setup empty string to start adding into
- string str = Reverse(current.Value.ToString());
- string endStr = "";
- //Initialize i to 0. i is our variable keeping track of which character we're on.
- int i = 0;
- //foreach character in our reversed string, I could of gone just with a for loop, but I'm lazy
- foreach (char c in str) {
- //If the character we're at is the one after we want to put a comma, first thousands place, first millions, etc
- if (i % 3 == 0 && i != 0) {
- //Add a comma to our string we are building
- endStr += ",";
- }
- //Add the current character to our string, and increment i
- endStr += c;
- i++;
- }
- //Reverse our size string.
- endStr = Reverse(endStr);
- //If we haven't set out padding size, set it now to the size of our build string.
- if (padSize == 0) {
- padSize = endStr.Length;
- }
- //Print out the directory size. with PadLeft on the string, and then after two spaces print the directory.
- Console.WriteLine("{0} {1}", endStr.PadLeft(padSize), current.Key);
- }
- }
- }
- }
- class WeekThirteenProblemOne {
- //Just test main.
- public static void Main() {
- Console.Write("Please enter a directory: ");
- string directory = Console.ReadLine();
- Console.WriteLine("\n\n");
- DirectorySize dir = new DirectorySize(directory);
- dir.printSizes();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement