DevTips.NET

Bepaal omvang directories

Dit stukje code levert een uitvoerbaar programma om de omvang van folders weer te geven. De eerste parameter geeft de folder op. Met de tweede parameter kan je oplopend en aflopend sorteren. Via de derde parameter kan je opgeven de top n van folders tonen. Na compilatie is het programma uit te voeren als: dirsize c:\windows\temp -sd 10 Dit geeft de omvang van de eerste 10 subfolders vanaf c:\windows\temp, aflopend gesorteerd op grootte. Het project, inclusief een gecompileerde versie, is bijgevoegd.

.NET

Code

using System;
using System.Data;
using System.IO;
 
namespace Sc.QuickTools
{
    class DirectorySize
    {
        private DataTable m_directoryTable;
        public DataTable DirectoryTable
        {
            get { return m_directoryTable; }
        }
 
        public DirectorySize()
        {
            m_directoryTable = new DataTable();
            m_directoryTable.Columns.Add("path");
            m_directoryTable.Columns.Add("size", typeof(long));
        }
 
        public long SetDirSize(string path)
        {
            long size = 0;
 
            DirectoryInfo dir = new DirectoryInfo(path);
 
            try
            {
                foreach (DirectoryInfo subFolder in dir.GetDirectories())
                {
                    size += SetDirSize(subFolder.FullName);
                }
 
                foreach (FileInfo file in dir.GetFiles())
                {
                    size = size + file.Length;
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("Error: {0}", ex.Message);
                Console.WriteLine(msg);
            }
 
            m_directoryTable.Rows.Add(new object[] { path, size });
 
            return size;
        }
    }
 
    class Program
    {
        public struct Param
        {
            public string RootFolder;
            public string SortOrder;
            public int TopCount;
 
            public Param(string[] args)
            {
                RootFolder = "";
                SortOrder = "size desc";
                TopCount = int.MaxValue;
 
                if (args.Length == 0)
                {
                    Console.WriteLine("Display (sub)folder sizes in KB.\n");
                    Console.WriteLine("dirsize [drive:][path][filename] [-sa|sd] [n]\n");
                    return;
                }
                if ((args.Length) > 0) RootFolder = args[0];
                if ((args.Length) > 1)
                {
                    if (args[1] == "-sa") SortOrder = "size asc";
                    if (args[1] == "-sd") SortOrder = "size desc";
                }
                if ((args.Length) > 2) TopCount = int.Parse(args[2]);
            }
        }
 
        static void Main(string[] args)
        {
            // arg[0] is root dir  
            // arg[1] is sort asc / desc
            // arg[2] is top x
 
            TimeSpan startTime = DateTime.Now.TimeOfDay;
 
            DirectorySize dirsize = new DirectorySize();
            Param parameter = new Param(args);
            if (parameter.RootFolder == "") return;
            dirsize.SetDirSize(parameter.RootFolder);
            dirsize.DirectoryTable.DefaultView.Sort = parameter.SortOrder;
            int i = 0;
            foreach (DataRowView row in dirsize.DirectoryTable.DefaultView)
            {
                i++;
                if (i == parameter.TopCount) break;
                decimal size = Convert.ToInt64(row[1]) / (1024 ^ 2);
                Console.WriteLine("{0} {1}",
                (size.ToString("N0") + " KB").PadRight(15, ' '), row[0]);
            }
            Console.WriteLine(" {0} Folder(s)",
            dirsize.DirectoryTable.DefaultView.Count);
 
            double processingTime =
            DateTime.Now.TimeOfDay.Subtract(startTime).TotalMilliseconds;
            Console.WriteLine(" Processing time: {0} seconds",
            (processingTime / 1000).ToString("N3"));
        }
    }
}
comments powered by Disqus