如何按文件名和编号排序文件列表? [英] How to order list of files by file name with number?

查看:395
本文介绍了如何按文件名和编号排序文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据其类型在目录中存储一堆文件.一旦有了它们,我想按文件名对其进行排序(其中有一个数字,我想以这种方式进行排序)

I have a bunch of files in a directory that I am trying to get based off their type. Once I have them I would like to order them by file name (there is a number in them and I would like to order them that way)

我返回的文件是:

file-1.txt
file-2.txt
...
file-10.txt
file-11.txt
...
file-20.txt

但是我得到他们的顺序看起来更接近这个:

But the order I get them in looks something more closely to this:

file-1.txt
file-10.txt
file-11.txt
...
file-2.txt
file-20.txt

现在我正在使用Directory.GetFiles()并尝试使用linq OrderBy属性.但是,与上面的第一个列表一样,我在排序文件列表时需要做的工作非常失败.

Right now I am using Directory.GetFiles() and attempting to using the linq OrderBy property. However, I am failing pretty badly with what I would need to do to order my list of files like the first list above.

Directory.GetFiles()似乎正在返回字符串列表,因此我无法获取文件属性(例如filenamename)的列表.

Directory.GetFiles() seems to be returning a list of strings so I am unable to get the list of file properties such as filename or name.

这是我当前的代码:

documentPages = Directory.GetFiles(documentPath, "*.txt").OrderBy(Function(p) p).ToList()

有人有什么想法吗?

推荐答案

我假设file.txt部分是可变的,这里只是作为文件名和类型可以变化的占位符.

I'm assuming the file and .txt parts are mutable, and just here as placeholders for file names and types that can vary.

我不经常使用正则表达式,因此这可能需要做一些工作,但这绝对是您需要遵循的方向:

I don't use regular expressions very often, so this may need some work yet, but it's definitely the direction you need to go:

Dim exp As String = "-([0-9]+)[.][^.]*$"
documentPages = Directory.GetFiles(documentPath, "*.txt").OrderBy(Function(p) Integer.Parse(Regex.Matches(p, exp)(0).Groups(1).Value)).ToList()


再次查看,我发现我想念您正在按*.txt文件进行过滤,这可以帮助我们缩小表达式的范围:


Looking again, I see I missed that you are filtering by *.txt files, which can help us narrow the expression:

Dim exp As String = "-([0-9]+)[.]txt$"

另一个包含测试数据的答案带来的另一个可能的改进是允许-和数字之间的空格:

Another possible improvement brought by the other answer that includes test data is to allow for whitespace between the - and numerals:

Dim exp As String = "-[ ]*([0-9]+)[.]txt$"


进一步值得注意的是,如果存在不遵循该模式的文本文件,则上述 将会失败.我们可以根据需要对此进行说明:


It's further worth noting that the above will fail if there are text files that don't follow the pattern. We can account for that if needed:

Dim exp As String = "-[ ]*([0-9]+)[.][^.]*$"
Dim docs = Directory.GetFiles(documentPath, "*.txt")
documentPages = docs.OrderBy(
     Function(p) 
            Dim matches As MatchCollection = Regex.Matches(p, exp)
            If matches.Count = 0 OrElse matches(0).Groups.Count < 2 Then Return 0
            Return Integer.Parse(matches(0).Groups(1).Value)
     End Function).ToList()

您还可以使用Integer.MaxValue作为默认选项,具体取决于您是希望它们出现在列表的开头还是结尾.

You could also use Integer.MaxValue as your default option, depending on whether you want those to appear at the beginning or end of the list.

这篇关于如何按文件名和编号排序文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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