简体   繁体   English

在同一目录中查找多个文件

[英]Find multiple files in the same directory

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. 我试图找到一个路径,在同一目录中具有相同文件名但具有不同扩展名( .bak.dwg )的文件列表。

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: 它仅按文件名分组...我尝试使用此代码进行修复,但结果为0:

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 谢谢

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 在.net 4.6上测试

System.IO.Path doesn't have a GetLongPath method. System.IO.Path没有GetLongPath方法。 I suspect you are using an external library like AlphaFS. 我怀疑您正在使用像AlphaFS这样的外部库。 In any case, GetLongPath returns the full file path, not the path of the file's folder. 无论如何, GetLongPath返回完整的文件路径,而不是文件文件夹的路径。

The file's folder path is returned by GetDirectoryName both in System.IO and other libraries like AlphaFS. 该文件的文件夹路径由System.IO和其他库(如AlphaFS)中的GetDirectoryName返回。 The following snippet will return only Dir1\\filename1 , Dir1\\filename2 and DifferentDir\\filename1 以下代码段仅返回Dir1\\filename1Dir1\\filename2DifferentDir\\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()
                    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual C#:将具有相同扩展名的多个文件移动到另一个目录中 - Visual C#: Move multiple files with the same extensions into another directory 按日期查找目录中的文件 - Find files in a directory by Date 查找使用 VSTO 部署的文件目录 - Find directory of files that are deployed with a VSTO 在同一目录中具有FileSystemWatcher的多个服务器 - Multiple servers with a FileSystemWatcher on same directory 视窗。 服务多个实例处理来自同一单个目录重复的文件 - Windows. Service multiple instances processing files from same single directory duplication 当多个线程生成的进程将文件复制到同一目录C#时缺少文件条目 - Missing file entries when multiple threads-generated processes copy files to same directory C# “CS8700:多个分析仪配置文件不能在同一目录中”,但只有一个 StyleCop 文件 - "CS8700: Multiple analyzer config files cannot be in the same directory" but only one StyleCop file 同时移动文件和创建目录 - Moving files and create directory at same time 通过名称而不是大小在目录中查找相同的文件 - Finding same files in directory by name and not size 更好的方法来查找目录中的所有文件 - Better Way to find all Files in Directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM