sorting file names

Posted on 2013-09-09
Medium Priority
Last Modified: 2014-02-11
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.
Question by:josephdts
  • 2
  • 2
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39478235
I would use the ISO format of "2013-08-13T10:39:32" because it works in alpha-numeric sort order without an external program.
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39478385
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.

Author Comment

ID: 39478528
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.
LVL 62

Accepted Solution

Julian Hansen earned 800 total points
ID: 39479300
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


Author Closing Comment

ID: 39852296
worked for me

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question