Skip to content

Instantly share code, notes, and snippets.

@alfeg
Created November 30, 2012 15:23
Show Gist options
  • Save alfeg/4176397 to your computer and use it in GitHub Desktop.
Save alfeg/4176397 to your computer and use it in GitHub Desktop.
Get file sizes by there extensions
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileHlpr
{
internal class FileInformation
{
public string Filename { get; set; }
public string Ext { get; set; }
public long Size { get; set; }
}
internal class FileResult
{
public string Ext { get; set; }
public long SummarySize { get; set; }
public long Count { get; set; }
}
public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter)) return this;
return null;
}
private const string fileSizeFormat = "fs";
private const Decimal OneKiloByte = 1024M;
private const Decimal OneMegaByte = OneKiloByte * 1024M;
private const Decimal OneGigaByte = OneMegaByte * 1024M;
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if (format == null || !format.StartsWith(fileSizeFormat))
{
return defaultFormat(format, arg, formatProvider);
}
if (arg is string)
{
return defaultFormat(format, arg, formatProvider);
}
Decimal size;
try
{
size = Convert.ToDecimal(arg);
}
catch (InvalidCastException)
{
return defaultFormat(format, arg, formatProvider);
}
string suffix;
if (size > OneGigaByte)
{
size /= OneGigaByte;
suffix = "GB";
}
else if (size > OneMegaByte)
{
size /= OneMegaByte;
suffix = "MB";
}
else if (size > OneKiloByte)
{
size /= OneKiloByte;
suffix = "kB";
}
else
{
suffix = " B";
}
string precision = format.Substring(2);
if (String.IsNullOrEmpty(precision)) precision = "2";
return String.Format("{0:N" + precision + "}{1}", size, suffix);
}
private static string defaultFormat(string format, object arg, IFormatProvider formatProvider)
{
IFormattable formattableArg = arg as IFormattable;
if (formattableArg != null)
{
return formattableArg.ToString(format, formatProvider);
}
return arg.ToString();
}
}
public static class ExtensionMethods
{
public static string ToFileSize(this long l)
{
return String.Format(new FileSizeFormatProvider(), "{0:fs}", l);
}
}
class Program
{
static void Main(string[] args)
{
var files = System.IO.Directory.EnumerateFiles(Directory.GetCurrentDirectory(), "*.*", SearchOption.AllDirectories);
var maps = from fileName in files.Where(file => !file.Contains(".hg"))
select new FileInformation()
{
Filename = fileName,
Ext = Path.GetExtension(fileName).ToLowerInvariant(),
Size = new FileInfo(fileName).Length
};
var res = from map in maps
group map by map.Ext
into g
select new FileResult()
{
Ext = g.Key,
Count = g.Count(),
SummarySize = g.Sum(f => f.Size)
};
foreach (var result in res.OrderByDescending(r => r.SummarySize))
{
Console.WriteLine("{0} Count: {1}, SummarySize: {2}", result.Ext, result.Count, result.SummarySize.ToFileSize());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment