tags:

views:

83

answers:

3

To get the details of files, Directory.Getfiles("DirectoryPath", "*.zip") is returning me the all the files in a directory. Each file has a DateTime stamp in the Filename as a Postfix:

e.g. {87fbf03b-ec94-44a0-aac5-ffbaf6416700}_20100204_145154634008919142146021.zip

I am splitting out the GUID from the above file name.

string filName = Path.GetFileNameWithoutExtension(testFile).Split('_')[0];

This explanation is just to tell you guys that thats how I can have more than one file with the same name in the same directory.

Now my question is How can i get the results same like Group by query in T-SQL? I need to know how many times a similar file name is there in that directory.

Is it possible through linq? Yes then how?

+1  A: 

Try this (not tested):

IList<string> fileNames = ...
var result = from fileName in fileNames
             group fileName by fileName.Split('_')[0] into grp
             select new
             {
                 FileName = grp.Key,
                 Count = grp.Count()
             };
Marcel Gosselin
Thanks. But I can't use Linq as i gotta stick with .NET Framework 2.0.
Novice
@user144842: From your initial post: "Is it possible through linq?" From your comment: "I can't use Linq" yet you accepted an answer that uses LINQ. That just doesn't make any sense. I've edited my answer to provide a version that does not use LINQ.
Jason
@Jason. Sorry Jason. I tried this solution in a new app (.net 3.5 framework), it worked fine. But when i stick this code in my old application, then i realized oh, Linq does not work with .NET framework 2.0. And top of it, i can't change my application to 3.5, as this app is already in use at 100s locations, really hard to upgrade them to .net framework 3.5. Though your solution, is also working. Thanks.
Novice
@Jason, wait a sec lemme try your edited code
Novice
Perhaps you could use LINQBridge http://www.albahari.com/nutshell/linqbridge.aspx
Nissan Fan
+2  A: 

Sure, use Enumerable.GroupBy:

var groups = from f in Directory.GetFiles("DirectoryPath", "*.zip")
             group f by f.Split('_')[0] into g
             select new {
                 GUID = g.Key
                 Count = g.Count()
             };
foreach(var group in groups) {
    Console.WriteLine("Guid = {0}: Count = {1}", group.GUID, group.Count);
}

It just reads so beautifully.

Since you specified in a comment that you can not use LINQ:

Dictionary<string, int> dict = new Dictionary<string, int>();
foreach(string filename in Directory.GetFiles("DirectoryPath", "*.zip")) {
    string guid = filename.Split('_')[0];
    if(!dict.ContainsKey(guid)) {
        dict.Add(guid, 0);
    }
    dict[guid]++;
}

foreach(KeyValuePair<string, int> kvp in dict) {
    Console.WriteLine("Guid = {0}: Count = {1}", kvp.Key, kvp.Value);
}
Jason
Hey thanks. Problem solved
Novice
+1  A: 

Instead of string you should use Guid type.

var groups = from f in System.IO.Directory.GetFiles("DirectoryPath", "*.zip")
    group f by f.Split('_')[0] into g
    select new
    {
        GUID = new Guid(g.Key),
        Count = g.Count()
    };
62316e