FileSort in visual basic based TimeStamp mentioned

2019-09-01 18:41发布

问题:

how to sort the files in the directory based on the part of the file name?

File format:
prod_orders_XXX_<TimeStamp>.datXXX = symbol of the product and the length may varies.
<TimeStamp> = date and time

Multiple files for the same XXX are possible with different time stamps.

Here are some examples:

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 

I need to sort the files based on the time stamp mentioned as part of file name.

回答1:

This should do what your looking for. You'll need to make sure the Date format is correct (I guessed it..) and add some extra validation.

    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

Edit, sorry just noticed you wanted to sort them all...

    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

End Module



回答2:

Sorry for not providing a VB answer, but it can't be hard to convert it to VB.NET The following code will stort the array by the timestamp.

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();

UPDATE: VB version of the same code.

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()