我有一个目录,里面有许多Excel文件名为[MMM yyyy].xlsx:
Jan 2012. xlsx
2月2012. xlsx
……等等…
2022.可能xlsx
我有以下代码必须调整为按自然日期顺序排序,而不是按文件写入时间排序:
Dim dir = New System.IO.DirectoryInfo(MY_PATH).GetFiles("*.xlsx",
IO.SearchOption.TopDirectoryOnly).OrderBy(Function(x) x.LastWriteTime).Reverse
For Each f As System.IO.FileInfo In dir
//code
Next
谢谢
你可以这样做:
Dim dir = New DirectoryInfo(MY_PATH).
EnumerateFiles("*.xlsx", SearchOption.TopDirectoryOnly).
OrderByDescending(Function(x) DateTime.ParseExact(x.Name.Replace(".xlsx", ""), "MMM yyyy", Nothing)
For Each f As FileInfo In dir
' code
Next
或者如果你只需要循环中的文件名,这样可能会快一点:
Dim dir = New Directory(MY_PATH).
EnumerateFiles("*.xlsx", SearchOption.TopDirectoryOnly).
OrderByDescending(Function(x) DateTime.ParseExact(Path.GetFileNameWithoutExtension(x), "MMM yyyy", Nothing)
For Each f As String In dir
' code
Next