[英]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\\filename1
, Dir1\\filename2
和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()
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.