【发布时间】:2019-04-06 08:55:07
【问题描述】:
我在一个目录中有文件,文件名如下:
批处理 1.10.18.xlsx
批处理 2.10.18.xlsx
...
批处理 31.10.18.xlsx
如你所见,他们有这样的模式:Batch dd.mm.yy.xlsx
我需要按照文件名中这些日期的顺序处理它们。
到目前为止的代码:
private void processFiles(string BatchFilePath)
{
IOrderedEnumerable<string> fileEntries =
Directory.GetFiles(BatchFilePath, "Batch *.xlsx")
.OrderBy(f => GetFileDay(f));
foreach (string fileName in fileEntries)
{
Console.WriteLine("Processing File " + Path.GetFileName(fileName));
// Code that read and process files
}
}
private int GetFileDay(string file)
{
string s1= file.Substring(7, 2);
if (s1.Substring(1) == ".")
s1 = s1.Substring(0, 1);
return int.Parse(s1);
}
代码不起作用。它仍然给我的文件名称顺序错误,如下所示:
批次 25.10.18.xlsx
批处理 22.10.18.xlsx...
批处理 9.10.18.xlsx
批处理 3.10.18.xlsx
...
【问题讨论】:
-
向我们展示预期的订单和看到的订单
-
@John 我想“看到”的顺序是“批次 3.10.18”,然后是“批次 31.10.18”,然后是“批次 4.10.18”。
-
我们可以假设您无法控制文件本身的命名吗?
-
最佳答案也可能取决于文件的数量,这会阻止 UI 吗?
-
谁知道排序是如此有趣! - 一些非常复杂的解决方案。我认为我的建议非常简单易懂 - 但这只是我自己的反映:-)