Sign in to follow this  
Followers 0
GrofLuigi

Inventory by filetype

8 posts in this topic

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 files

You have YYY .doc files

....

You have ZZZ .bmp files

Where 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 by GrofLuigi
0

Share this post


Link to post
Share on other sites

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.cs

To use it, pass one or more directory names as arguments when you run it e.g.

whateveryouactuallycalledit.exe c:\users\yourusernamehere\downloads

And 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)

0

Share this post


Link to post
Share on other sites

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 by GrofLuigi
0

Share this post


Link to post
Share on other sites

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).

0

Share this post


Link to post
Share on other sites

It works as expected, thank you very much. :thumbup :thumbup :thumbup

I suggest this topic be moved to the programming forum, it might be useful to someone else.

GL

0

Share this post


Link to post
Share on other sites

You're welcome. Moved to programming forum.

0

Share this post


Link to post
Share on other sites

Ah - an oldie but goodie! Thanks allen for the memories.

Cheers and Regards

0

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.