Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

Inventory by filetype

- - - - -

  • Please log in to reply
7 replies to this topic

#1
GrofLuigi

GrofLuigi

    GroupPolicy Tattoo Artist

  • Member
  • PipPipPipPipPipPip
  • 1,369 posts
  • Joined 21-April 05
  • OS:none specified
  • Country: Country Flag
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, 21 April 2012 - 10:29 PM.



How to remove advertisement from MSFN

#2
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

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)
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#3
GrofLuigi

GrofLuigi

    GroupPolicy Tattoo Artist

  • Member
  • PipPipPipPipPipPip
  • 1,369 posts
  • Joined 21-April 05
  • OS:none specified
  • Country: Country Flag
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, 22 April 2012 - 09:47 AM.


#4
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag
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...r4374hzpcsh5x60 (virus-free of course).
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#5
GrofLuigi

GrofLuigi

    GroupPolicy Tattoo Artist

  • Member
  • PipPipPipPipPipPip
  • 1,369 posts
  • Joined 21-April 05
  • OS:none specified
  • Country: Country Flag
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

#6
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag
You're welcome. Moved to programming forum.
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#7
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,814 posts
  • Joined 13-January 06
But windirstat already do this for free.

#8
bphlpt

bphlpt

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,912 posts
  • Joined 12-May 07
  • OS:none specified
  • Country: Country Flag
Ah - an oldie but goodie! Thanks allen for the memories.

Cheers and Regards

Posted Image





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users