简体   繁体   English

检查文件名在FileInfo数组中是否存在多次

[英]Check if a file name exists more than once in a FileInfo array

FileInfo[] folderFiles = folder.GetFiles();

foreach (FileInfo file in folderFiles)
{

    int fileCount = 0;

    StreamWriter sw = null;

    string fileName = Path.GetFileNameWithoutExtension(file.Name);

    string[] brkedfilename = fileName.Split('_');

    string stringToCheck = brkedfilename[3];

    for (int i = 0; i <= folderFiles.Count() - 1; i++)
    {
        string fileName2 = folderFiles[i].Name;

        string[] brkedfilename2 = fileName2.Split('_');

        if (brkedfilename2[3] == stringToCheck)
        {
            fileCount = ++fileCount;

            if (fileCount == 2)
           {
                sw = new StreamWriter(folderPath + "/" + newFileName, true);
                sw.WriteLine(stringToCheck + "  " + "--" + "  " + "Repeated in folder " + " " + folder.Name);
                sw.Close();
            }
        }
    }
}

By doing this way, if a file name is existing 2 times , it is writing that file name 2 times. 通过这种方式,如果一个文件名存在2次,则它将写入该文件名2次。 But I want it to write only once. 但是我只想写一次。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance. 提前致谢。 :) :)

You can use LINQ to find files with same brkedNames: 您可以使用LINQ查找具有相同brkedNames的文件:

var repeatedFiles = from f in folderFiles
                    let brkedName = Path.GetFileNameWithoutExtension(f.Name).Split('_')[3]
                    group f by brkedName into g
                    where g.Count() > 1
                    select new
                    {
                        BrkedName = g.Key,
                        Count = g.Count(),
                        Files = g
                    };

This query groups all files by 4th part of splitted names (note: as in your original code I don't check if there is at least 4 parts) and then select groups with more than one file. 此查询按名称的第4部分对所有文件进行分组(请注意:与您的原始代码一样,我不会检查是否至少有4部分),然后选择包含多个文件的组。 It also provides some stats - brked name, count of files with same name and files collection. 它还提供了一些统计信息-代理名称,具有相同名称的文件计数和文件收集。

foreach(var group in repeatedFiles)
   sw.WriteLine($"{group.BrkedName} -- repeated {group.Count} times in {folder.Name}");

I propose use a hashset. 我建议使用哈希集。 Hashset cannot contain duplicate elements. 哈希集不能包含重复的元素。 When You add duplicate element do hashset method Add return false and program go on witout error but hashset will not take duplicate element 当您添加重复元素时,请执行hashset方法Add返回false ,程序将继续执行错误,但哈希集将不包含重复元素

var duplicate = new HashSet<string>();

    foreach (FileInfo file in folderFiles)
    {
        int fileCount = 0;

        StreamWriter sw = null;

        string fileName = Path.GetFileNameWithoutExtension(file.Name);

        string[] brkedfilename = fileName.Split('_');

        string stringToCheck = brkedfilename[3];

         if (!duplicate.Add(stringToCheck))
            {
                sw = new StreamWriter(folderPath + "/" + newFileName, true);
                sw.WriteLine(stringToCheck + "  " + "--" + "  " + "Repeated in folder " + " " + folder.Name);
                sw.Close();
            }
    }

Program adds each element do Hashset duplicate when element is in Hashset already Add metod return false when I know the element is in the Hashset and I save in the file as You want 计划将每个元素做Hashset复制当元素是Hashset已经Add梅托德返回false ,当我知道的元素在Hashset和我保存在文件中,只要你想

You can use Dictionary ( filename, repeat time) to list all filename exist. 您可以使用字典(文件名,重复时间)列出所有存在的文件名。 And after that you can loop the dictionary to write to console. 之后,您可以循环字典以写入控制台。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM