简体   繁体   English

C#文件列表名称比较

[英]C# list of files name comparison

I have a simple but yet very hard task for my coding skills. 对于我的编码技巧,我有一个简单但非常艰巨的任务。

Basically I have a task to parse all file names in specific folder (this executed ok) but then I have to compare those file names and choose the one with the latest (biggest) number in a specific part of file name. 基本上我有一个任务来解析特定文件夹中的所有文件名(这执行正常)但我必须比较这些文件名并选择文件名特定部分中具有最新(最大)编号的文件名。 An example: 一个例子:

0074-105-NVK.1.p7.ver.1.pdf
0074-105-NVK.1.p7.ver.2.pdf
0074-105-NVK.1.p7.ver.3.pdf

The part that interests me is the one where " ver.1 " from this the program should choose the highest one and remove other files. 我感兴趣的部分是“ ver.1 ”,程序应选择最高的那个并删除其他文件。 And I really don't know how to implement this filename comparison. 我真的不知道如何实现这个文件名比较。

I suggest extracting version info with a help of regular expressions and ordering by this version: 我建议在正则表达式的帮助下提取版本信息并按此版本排序:

  string[] files = new string[] {
     "0074 - 105 - NVK.1.p7.ver.1.pdf",
     "0074 - 105 - NVK.1.p7.ver.2.pdf",
     "0074 - 105 - NVK.1.p7.ver.3.pdf", };

  string pattern = @"ver\.(?<version>[0-9]+(\.[0-9]+)*)[^0-9]+$";

  var result = files
    .Select(file => new {
      name = file,
      ver = new Version(Regex.Match(file, pattern).Groups["version"].Value + ".0")
    })
    .OrderByDescending(item => item.ver)
    .Select(item => item.name)
    .FirstOrDefault();

You could use the following to get all file names as: 您可以使用以下命令获取所有文件名:

string[] fileNames = Directory.GetFiles(@"d:\Dir\", "*.pdf").ToList();
List<string> nameOnly = new List<string>();
List<KeyValuePair<string, string>> bind = new List<KeyValuePair<string, string>>();

once you have all the file names in a list then you can exclude the extension as: 一旦列表中包含所有文件名,您就可以将扩展名排除为:

foreach(var item in fileNames)
{ 
    var x = Regex.Match(item, @".*(?=\.)").Value;
    nameOnly.Add(x);
}

Just for binding each name with its file name: 仅用于将每个名称与其文件名绑定:

foreach(var item in nameOnly)
{ 
    var x = Regex.Match(item, @".*(?=\.)").Value;
    bind.Add(new KeyValuePair<string, string>(x,item));
}

And you can get the final file name with max number as: 您可以使用最大数字获取最终文件名:

var max = bind.OrderBy(x => x.Key);
var fileName = max.LastOrDefault().Value;

It can be done nicely with a single lambda row: 使用单个lambda行可以很好地完成它:

var s1 = "0074-105-NVK.1.p7.ver.1.pdf";
var s2 = "0074-105-NVK.1.p7.ver.2.pdf";
var s3 = "0074-105-NVK.1.p7.ver.3.pdf";
var arr = new[] {s1, s2, s3};
var latestVer = arr.OrderBy(s => int.Parse(s.Split('.')[s.Split('.').Length - 2])).Last();

You should have filenames in array or list: 你应该在数组或列表中有文件名:

var fileNames = new List<string>();
// fill list with proper file names

Regading you sample you can sort this list (assume that only ver.x can change): 在回复您的示例时,您可以对此列表进行排序(假设只有ver.x可以更改):

list.Sort(); // you can use parametrized method which sort by part of string

You can also use SortedList (that you have all sorted automatically): 您还可以使用SortedList(您已自动排序):

var fileNames = new SortedList<string>();
fileNames.Add(fileName.Substring(18), fileName);

Or you can use Linq: 或者你可以使用Linq:

var sorted = fileNames.OrderBy(item => item.Substring(18)); // in that case you can use different parts of file name)

Try this code: 试试这段代码:

       string[] files = new string[] {
                         "0074 - 105 - NVK.1.p7.ver.1.pdf",
                         "0074 - 105 - NVK.1.p7.ver.11.pdf",
                         "0074 - 105 - NVK.1.p7.ver.2.pdf", };


        var result = files.Select(f => new
        {
            name = f,
            ver = int.Parse((f.Substring(f.IndexOf(".ver") + 1).Split('.')[1]))
        })
          .OrderByDescending(x => x.ver)
          .Select(x => x.name)
          .FirstOrDefault();

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

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