如何获得从1个文件到n在一个特定的模式? [英] How to get files from 1 to n in a particular pattern?

查看:89
本文介绍了如何获得从1个文件到n在一个特定的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有这样的文件:

  NewFile.part01.zip
NewFile.part02.zip
NewFile.part04.zip
NewFile.part06.zip
NewFile.part07.zip
 

你如何获得这些文件在此图案,所以你只能得到所谓的NEWFILE单个文件,也得到缺少的整数,在这种情况下,(3,5)

现在我检查的文件一个接一个,如果该名称只在不同的后缀,然后跳绳,还检查数量+1超过previous等。

不过,我以为有人可能有这样做的更好,更优雅的方式。 LINQ中,正则表达式等?

编辑:

因此​​,为了知道什么时候连续文件端是当最后的文件大小具有比其他的差的方式。所以,它就像200MB,200MB,200MB,......,然后最后一个是196MB。

我输入的是完整的文件列表中包含的路径:

 C:\ NewFile.part01.zip
C:\ NewFile.part02.zip
...
 

解决方案

您可以使用正则表达式,看起来像这样

  ^(<名称> *)\一部分。(LT; NUM> \ D {0,})\ ZIP $。
 

这应该给你两个小组比赛,一个是文件名和一个用于NUM

做一个循环,收集信息,然后可以识别(在列表中存储)的名称和编号。如果你喜欢,你可以使用LINQ在循环像这样,以确定失踪人数设定

 的foreach(INT I = list.Min(); I< = list.Max();我++)
{
  如果(!list.Contains(i))的
    missingNums.Add(ⅰ);
}
 

---编辑给出的例子是要求

这是这个例子你将如何使用正则表达式来遍历文件列表

 字符串模式= @^(<名称> *)\部分(小于?NUM> \ D {0,})\拉链$。;
    的foreach(在文件中字符串的文件)
    {
        比赛比赛= Regex.Match(文件模式);
        如果(match.Success&安培;&安培; match.Groups.Count&GT = 2)
        {
            字符串文件名= match.Groups [名称]值。
            INT NUM = Convert.ToInt32(match.Groups [NUM]值。);
        }
    }
 

Suppose you have files like:

NewFile.part01.zip
NewFile.part02.zip
NewFile.part04.zip
NewFile.part06.zip
NewFile.part07.zip

How do you get the files in this patter so you only get a SINGLE file called "NewFile" and also get the missing ones as integers, in this case (3, 5)

Right now I am checking files one by one and if the name only differs in the suffix then skipping, also checking the number is +1 than the previous, etc.

But I thought someone might have a better, more elegant way of doing this. Linq, regex, etc?

EDIT:

So the way to know when the continuous files end is when the last file size has a difference than others. So it's like 200mb, 200mb, 200mb, ..., then the last one is 196mb.

My input is the full file list with the path like:

"C:\NewFile.part01.zip"
"C:\NewFile.part02.zip"
...

解决方案

You can use regex that looks like this

^(?<name>.*)\.part(?<num>\d{0,})\.zip$

which should give you two group matches, one for the filename and one for the num

Do a loop, collect the info and then you can identify the name and numbers (store in a list). If you like you can use linq in loop like this to identify the missing number set

foreach(int i = list.Min(); i <= list.Max(); i++)
{
  if (!list.Contains(i))
    missingNums.Add(i);
}

--- Edited to give example as requested

This is the example how you will use the regex to iterate through your file list

   string pattern = @"^(?<name>.*)\.part(?<num>\d{0,})\.zip$";
    foreach(string file in files)
    {
        Match match = Regex.Match(file, pattern);
        if (match.Success && match.Groups.Count >= 2)
        {
            string filename = match.Groups["name"].Value;
            int num = Convert.ToInt32(match.Groups["num"].Value);
        }
    }

这篇关于如何获得从1个文件到n在一个特定的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆