文件名中提到的基于Visual Basic的TimeStamp中的FileSort

时间:2012-01-27 12:11:48

标签: vb.net sorting filesort

如何根据文件名的一部分对目录中的文件进行排序?

文件格式:
prod_orders_XXX_<TimeStamp>.datXXX = symbol of the product and the length may varies.
<TimeStamp> = date and time

使用不同的时间戳可以为同一个XXX提供多个文件。

以下是一些例子:

prod_orders_abc_20122001083000.dat
prod_orders_abc_20122007083111.dat
prod_orders_xyz_20122003093157.dat
prod_orders_xyz_20122001083000.dat
prod_orders_abc_20122001163139.dat
prod_orders_abc_20122002093137.dat
prod_orders_xyz_20122001183000.dat
prod_orders_abc_20122001163139.dat
prod_orders_abc_20122001093137.dat 

我需要根据文件名中提到的时间戳对文件进行排序。

2 个答案:

答案 0 :(得分:1)

很抱歉没有提供VB答案,但将其转换为VB.NET并不困难 以下代码将按时间戳对数组进行调试。

string[] fileNames = 
{ "prod_orders_abc_20122001083000.dat",
  "prod_orders_abc_20122007083111.dat",
  "prod_orders_xyz_20122003093157.dat",
  "prod_orders_xyz_20122001083000.dat",
  "prod_orders_abc_20122001163139.dat",
  "prod_orders_abc_20122002093137.dat",
  "prod_orders_xyz_20122001183000.dat",
  "prod_orders_abc_20122001163139.dat",
  "prod_orders_abc_20122001093137.dat" 
};

var result = fileNames.OrderBy(s => s.Substring(s.Length - 12,4)).ToArray();

更新: VB版本的代码相同。

Dim fileNames As String() = {"prod_orders_abc_20122001083000.dat", "prod_orders_abc_20122007083111.dat", "prod_orders_xyz_20122003093157.dat", "prod_orders_xyz_20122001083000.dat", "prod_orders_abc_20122001163139.dat", "prod_orders_abc_20122002093137.dat", _
    "prod_orders_xyz_20122001183000.dat", "prod_orders_abc_20122001163139.dat", "prod_orders_abc_20122001093137.dat"}

Dim result = fileNames.OrderBy(Function(s) s.Substring(s.Length - 12, 4)).ToArray()

答案 1 :(得分:0)

这应该做你想要的。您需要确保日期格式正确(我猜对了..)并添加一些额外的验证。

    Dim filename As String = "prod_orders_abc_20122001083000.dat"

    filename = IO.Path.GetFileNameWithoutExtension(filename.Split("_").Last)

    'yyyyddMMhhmmss
    '20122001083000
    Dim fileDate As Date

    If Date.TryParseExact(filename, "yyyyddMMhhmmss", Globalization.CultureInfo.CurrentCulture, Globalization.DateTimeStyles.None, fileDate) Then
        Debug.WriteLine(fileDate)
    Else
        Debug.WriteLine("unable to get date")
    End If

编辑,抱歉只是注意到你想要对它们进行排序......

    Sub main()

    Dim filenames() As String =
        {"prod_orders_abc_20122001083000.dat",
         "prod_orders_abc_20122007083111.dat",
         "prod_orders_xyz_20122003093157.dat",
         "prod_orders_xyz_20122001083000.dat",
         "prod_orders_abc_20122001163139.dat",
         "prod_orders_abc_20122002093137.dat",
         "prod_orders_xyz_20122001183000.dat",
         "prod_orders_abc_20122001163139.dat",
         "prod_orders_abc_20122001093137.dat"}


    Dim SortedFileNames As List(Of String) =
        filenames.OrderBy(Function(fileName) GetDateFromFileName(fileName)).ToList


End Sub

Private Function GetDateFromFileName(fileName As String) As Date

    fileName = IO.Path.GetFileNameWithoutExtension(fileName.Split("_").Last)

    'yyyyddMMhhmmss
    '20122001083000
    Dim fileDate As Date

    If Date.TryParseExact(fileName, "yyyyddMMhhmmss", Globalization.CultureInfo.CurrentCulture, Globalization.DateTimeStyles.None, fileDate) Then
        Return fileDate
    Else
        Return Date.MinValue
    End If

End Function

结束模块