sorting file names

I have a number of files in a directory folder of the type
As you can see, there is a date/time stamp in the names.
I want to write a small executable that will sort these files by the data/time stamps.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
I would use the ISO format of "2013-08-13T10:39:32" because it works in alpha-numeric sort order without an external program.
Dave BaldwinFixer of ProblemsCommented:
And another thought.  If you are viewing this file list in Windows Explorer, it will sort the names in it's own odd way no matter what the 'order' in the directory is.
josephdtsAuthor Commented:
I cannot use ISO format. This is how these files appear.
I would like the output of the executable to be a text file with the names in the correct order.
Julian HansenCommented:
Try this - code below will sort files by extracting the date information from the string. Parsing to a DateTime object and then prepending the ticks for the date to the file name which is used to sort the filenames list.
When output is created remove the prefix from the filename
Module Module1
    Sub Main()
        Dim DateString As String
        Dim dt As DateTime
        Dim parts As String()
        Dim files As New List(Of String)()
        Dim folder As String

        folder = "C:\input"

        ' Read files from folder
        For Each foundFile As String In My.Computer.FileSystem.GetFiles(folder)
            DateString = foundFile.Substring(folder.Length+ 8, 18)
            parts = foundFile.Split(New [Char]() {"-"c, "."c})
            ' Extract date
            DateString = parts(2) & "/" & parts(3) & "/" & parts(4) & " " & parts(5) & ":" & parts(6) & ":" & parts(7)
            dt = DateTime.Parse(DateString)
            ' pre-pend ticks to filename to provide a sort prefix and add to list of found files
            files.Add(dt.Ticks() & ":" & foundFile)
        ' Sort the result
        ' Output files to console after removing prefix
        For Each f As String In files
            Console.WriteLine(f.Substring(f.IndexOf(":") + folder.Length + 2))
        ' sort collection
        ' output and remove prefix
    End Sub
End Module

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
josephdtsAuthor Commented:
worked for me
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.