getting total number of every extension in whole directory tree
we have a hardrive with hundreds of thousands of files
i need to figure out how many of every file extension we have
how can i do this with c#?
i need it to go through every directory. this lawyers at my company need this. it can be a total f开发者_开发技巧or the entire hardrive it does not have to be broken down by directory
example:
1232 JPEG
11 exe
45 bat
2342 avi
532 doc
Using LINQ:
string path = @"D:\folder";
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
.GroupBy(p => Path.GetExtension(p));
foreach (var item in files)
Console.WriteLine(item.Key + " : " + item.Count());
Edit: And recursively to loop through the sub directories:
Dictionary<string, int> extensions = new Dictionary<string, int>();
Action<string> CalcFilesCount = null;
CalcFilesCount = f =>
{
var files = Directory.GetFiles(f).GroupBy(p => Path.GetExtension(p));
foreach (var ex in files)
{
if (extensions.Keys.Contains(ex.Key))
extensions[ex.Key] += ex.Count();
else
extensions[ex.Key] = ex.Count();
}
foreach (var p2 in Directory.GetDirectories(f))
CalcFilesCount(p2);
};
CalcFilesCount(path);
foreach (var item in extensions)
Console.WriteLine(item.Key + " :" + item.Value);
Good luck!
Simply use :
var folder = Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments // for example
);
var allExt = from f in Directory.GetFiles(
folder,
"*.*",
SearchOption.AllDirectories // The key concept is here
)
group f by Path.GetExtension(f) into fileWithExt
let Count = fileWithExt.Count()
orderby Count descending
select new { Ext = fileWithExt.Key, Count}
;
foreach (var item in allExt)
{
Console.WriteLine("{0} : {1}", item.Ext, item.Count);
}
[Edit] did not see the count requirement.... code updated
If you don't care about memory consumption, try this:
string[] files = Directory.GetFiles(@"C:\", "*.*", SearchOption.AllDirectories);
foreach (var extension in files.GroupBy(Path.GetExtension))
{
Console.WriteLine("{0}: {1} file(s)", extension.Key, extension.Count());
}
Console.ReadLine();
If you do care, you'll need to build an recursive algorithm to navigate through those folders, like this:
static Dictionary<string, int> extensions = new Dictionary<string, int>();
static void Main(string[] args)
{
recurseFolders(@"c:\");
foreach (KeyValuePair<string, int> extension in extensions)
Console.WriteLine("{0}: {1} file(s)", extension.Key, extension.Value);
Console.ReadLine();
}
private static void recurseFolders(string path)
{
string[] files= Directory.GetFiles(path,"*.*",SearchOption.TopDirectoryOnly);
foreach (var extension in files.GroupBy(Path.GetExtension))
{
if(!extensions.ContainsKey(extension.Key))
extensions.Add(extension.Key, extension.Count());
else
extensions[extension.Key] += extension.Count();
}
foreach (string directory in Directory.GetDirectories(path))
recurseFolders(directory);
}
I don't know if c# is a requirement, but you can also achieve the same result with a powershell command :
Get-ChildItem -Recurse | Group-Object -Property extension
精彩评论