0

I'm trying to find, giving a path, a list of files that have same filename but different extensions (.bak and .dwg) in the same directory.

I have this code:

String[] FileNames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".bak") || s.EndsWith(".dwg")).ToArray();
var queryDupNames = from f in FileNames
                    group f by Path.GetFileNameWithoutExtension(f) into g
                    where g.Count() > 1                                            
                    select new { Name = g.Key, FileNames = g };

This works great to locate files with the same filename but in the whole system. I need only to obtain those that are in the same directory.

For example:

- Dir1\filename1.bak
- Dir1\filename1.dwg
- Dir1\filename2.bak
- Dir1\filename2.dwg
- Dir1\filename3.dwg
- DiferentDir\filename1.bak
- DiferentDir\filename1.dwg
- DiferentDir\filename3.dwg

The result should be:

- Dir1\filename1.bak
- Dir1\filename1.dwg
- Dir1\filename2.bak
- Dir1\filename2.dwg
- DiferentDir\filename1.bak
- DiferentDir\filename1.dwg

But with my code, filename3 is also included due to

g.count() > 1

it's true. It's grouping by only filename... I tried to fix with this code but I got 0 results:

String[] FileNames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".bak") || s.EndsWith(".dwg")).ToArray();
var queryDupNames = from f in FileNames
                    group f by new { path = Path.GetLongPath(f), filen = Path.GetFileNameWithoutExtension(f) } into g
                    where g.Count() > 1                                            
                    select new { Name = g.Key, FileNames = g };

Any help or clue?

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kioko Kiaza
  • 1,378
  • 8
  • 28
  • 57

2 Answers2

0

first find all folders.

then for each folder find all the files with same name but different extension. something like this:

        var list = new List<string>();
        foreach (var subDirectory in Directory.EnumerateDirectories(@"C:\Temp"))
        {
            var files = Directory.EnumerateFiles(subDirectory);
            var repeated = files.Select(Path.GetFileNameWithoutExtension)
                .GroupBy(x => x)
                .Where(g => g.Count() > 1)
                .Select(y => y.Key);
            list.AddRange(repeated);
        }

tested on .net 4.6

Nahum
  • 6,959
  • 12
  • 48
  • 69
  • The OP already has all files from all folders, that's not the problem. The question asks how to group by folder and extension, and why the current approach fails – Panagiotis Kanavos Aug 11 '15 at 08:38
  • @PanagiotisKanavos it´s right. Your code is interesting but I can´t use it because I need to support long path names and multiple subfolders with Delimon dll http://stackoverflow.com/questions/31919748/directory-getfiles-stops-searching-when-error/31920319#31920319 – Kioko Kiaza Aug 11 '15 at 08:55
0

System.IO.Path doesn't have a GetLongPath method. I suspect you are using an external library like AlphaFS. In any case, GetLongPath returns the full file path, not the path of the file's folder.

The file's folder path is returned by GetDirectoryName both in System.IO and other libraries like AlphaFS. The following snippet will return only Dir1\filename1, Dir1\filename2 and DifferentDir\filename1

var files = new[]
{
    @"c:\Dir1\filename1.bak",
    @"c:\Dir1\filename1.dwg",
    @"c:\Dir1\filename2.bak",
    @"c:\Dir1\filename2.dwg",
    @"c:\Dir1\filename3.dwg",
    @"c:\DiferentDir\filename1.bak",
    @"c:\DiferentDir\filename1.dwg",
    @"c:\DiferentDir\filename3.dwg",
};

var duplicates =   from file in files
                    group file by new
                    {
                        Folder = Path.GetDirectoryName(file),
                        Name = Path.GetFileNameWithoutExtension(file)
                    } into g
                    where g.Count()>1
                    select new
                    {
                        Name = g.Key,
                        Files = g.ToArray()
                    };
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236