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

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

问题描述

我在一个目录中有一堆文件,我试图根据它们的类型来获取这些文件.一旦我有了它们,我想按文件名对它们进行排序(其中有一个数字,我想这样排序)

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天全站免登陆