GrofLuigi Posted April 22, 2012 Share Posted April 22, 2012 (edited) I need a program (may also be batch file or any other script) which will let me select a directory and then produce a listing saying:You have XXX .txt filesYou have YYY .doc files....You have ZZZ .bmp filesWhere XXX > YYY > ZZZ and the program will need to figure that out, and also to list ALL filetypes.Optionally, let me select more than one directory. Mandatory: Recurse subdirectories.Does any such thing exist?GL Edited April 22, 2012 by GrofLuigi Link to comment Share on other sites More sharing options...
CoffeeFiend Posted April 22, 2012 Share Posted April 22, 2012 Does any such thing exist?I'm not aware of one, but it would be very quick and easy to write one in C#, relying on LINQ to do 99% of the work for us. A couple lines to iterate through folder names passed as an arg, one line to get all files inside it (including subfolders), one LINQ query to group the data by extension (returning extension and count i.e. extract what you want) then you only have to write it to the console. It would take about 10 statements for the whole thing.Edit: bored. Here's the whole thing (a simple 5 minute console app) written in C#:using System;using System.IO;using System.Linq;namespace DirExtCount{ class Program { static void Main(string[] args) { foreach (var arg in args) if (Directory.Exists(arg)) ProcessDirectory(arg); } static void ProcessDirectory(string dirName) { var dir = new DirectoryInfo(dirName); var fileList = dir.GetFiles("*.*", SearchOption.AllDirectories); //this is where the LINQ magic happens var qry = from file in fileList group file by file.Extension.ToLower() into grp orderby grp.Count() descending select new { Extension = grp.Key, Count = grp.Count() }; Console.WriteLine("Extension Count for directory: " + sDir); foreach (var res in qry) Console.WriteLine("You have {0} {1} files", res.Count, res.Extension); } }}Save as whateveryouwannacallit.cs then compile using:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" whateveryouwannacallit.csTo use it, pass one or more directory names as arguments when you run it e.g.whateveryouactuallycalledit.exe c:\users\yourusernamehere\downloadsAnd if you wanted it to actually list the extension counts of multiple folders combined together instead of separately, that'd only be a couple more lines. Even making it a full GUI app would be really quick (fire a BackgroundWorker thread to do the heavy work, then databind the results to a simple ListView or similar) Link to comment Share on other sites More sharing options...
GrofLuigi Posted April 22, 2012 Author Share Posted April 22, 2012 (edited) Thank you!Unfortunately, it doesn't compile. Says:Inventory.cs(3,14): error CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)I have client profile. May that be the reason?Edit: In .Net 4.0 I have System.Xml.Linq.dll and System.Data.Linq.dll. I don't have any programming languages installed.GL Edited April 22, 2012 by GrofLuigi Link to comment Share on other sites More sharing options...
CoffeeFiend Posted April 22, 2012 Share Posted April 22, 2012 Hmm. Strange. It compiles fine as-is here (Win7 x64). I do have VS2010 installed but that shouldn't change a thing. It should compile fine, as-is with the compiler installed by the .NET framework. I'll probably try on a mostly clean VM later (might have to add a /reference param or something). Meanwhile, here's the compiled version: http://www.mediafire.com/?r4374hzpcsh5x60 (virus-free of course). Link to comment Share on other sites More sharing options...
GrofLuigi Posted April 22, 2012 Author Share Posted April 22, 2012 It works as expected, thank you very much. :thumbup I suggest this topic be moved to the programming forum, it might be useful to someone else.GL Link to comment Share on other sites More sharing options...
CoffeeFiend Posted April 22, 2012 Share Posted April 22, 2012 You're welcome. Moved to programming forum. Link to comment Share on other sites More sharing options...
allen2 Posted April 23, 2012 Share Posted April 23, 2012 But windirstat already do this for free. Link to comment Share on other sites More sharing options...
bphlpt Posted April 23, 2012 Share Posted April 23, 2012 Ah - an oldie but goodie! Thanks allen for the memories.Cheers and Regards Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now