6 Replies - 959 Views - Last Post: 28 August 2013 - 11:05 PM Rate Topic: -----

#1 qgzn9858  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-August 13

Sorting multiple text files in order of date/time

Posted 28 August 2013 - 03:07 PM

hello,
i am having trouble figuring out how to sort text files by order of date/time which is saved on a certain line in multiple text files, the format the date and time that is saved as is the same format as the computers date/time "Date.Now.ToString" such as "28/08/2013 6:49:11 PM".

once it sorts the text files by the date and time in order it lists the file names in a list box in order of oldest on top with the newest on the bottom.

if someone is able to help me with what code to use it would he a massive help for me.

thanks, if you need some extra info from me for what i need please ask. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting multiple text files in order of date/time

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Sorting multiple text files in order of date/time

Posted 28 August 2013 - 04:26 PM

What aspect of this are you have problems with? What code have come with so far?
Was This Post Helpful? 0
  • +
  • -

#3 qgzn9858  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-August 13

Re: Sorting multiple text files in order of date/time

Posted 28 August 2013 - 05:49 PM

    Private Sub collectsuburb()
        Dim l_Dir As IO.DirectoryInfo
        Dim selectedsuburb As String
        Dim fname As String
        Dim sname As String
        Dim numberoffile As Integer = 0
        Dim Suburbpresent As New Collection
        Dim timeOfOrder As String

        Dim pathway As String = path & "\Suburb.txt"
        Dim suburb As New System.IO.StreamWriter(pathway, False)

        If IO.Directory.Exists(path & "\Orders") Then ' check if location exisits so program does not crash
            l_Dir = New IO.DirectoryInfo(path & "\Orders") ' define the database file location

            For Each l_File As IO.FileInfo In l_Dir.GetFiles ' for every file(name) in the file directory that is a .txt file
                If l_File.Extension.ToUpper = ".TXT" Then 'only checks and opens .txt files
                    Dim filename As String = path + "\Orders\" + l_File.ToString
                    Dim objreader As New System.IO.StreamReader(filename)
                    'open information from text file, reades the lines until where the suburb saved location is entered
                    fname = objreader.ReadLine() ' first name
                    sname = objreader.ReadLine() ' surname
                    objreader.ReadLine() ' street
                    selectedsuburb = objreader.ReadLine() ' suburb
                    objreader.ReadLine() 'postcode
                    objreader.ReadLine() 'mobile number
                    objreader.ReadLine() 'home number
                    objreader.ReadLine() 'business number
                    objreader.ReadLine() 'order
                    objreader.ReadLine() 'quantity
                    timeOfOrder = objreader.ReadLine() 'time of order DD/MM/YYYY HH:MM:SS AM/PM
                    'wrtie all suburbs to a single text file
                    suburb.WriteLine(selectedsuburb)
                    objreader.Close() ' close the opened customer file

                End If
            Next

        End If
        suburb.Close() ' close the suburb folder
    End Sub

^^^so far this is what i have to read each text file in the desired location, i want it to sort the text files names in order of oldest time of order on top to the newest time of order on the bottom.

here is what i am using to put the text file names into the list box but it is unsorted
        Dim ordernumber As Integer = 0
        lstDeliveries.Items.Clear()
        Dim l_Dir As IO.DirectoryInfo

        If IO.Directory.Exists(path & "\Orders") Then 'checks that file location exists
            l_Dir = New IO.DirectoryInfo(path & "\Orders") ' define the database file location

            For Each l_File As IO.FileInfo In l_Dir.GetFiles  ' for every file(name) in the file directory that is a .txt file
                If l_File.Extension.ToUpper = ".TXT" Then 'only checks and opens .txt files

                    lstDeliveries.Items.Add(l_File.ToString.Substring(0, l_File.ToString.Length - 4))
                    ordernumber += 1

                End If
            Next

Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Sorting multiple text files in order of date/time

Posted 28 August 2013 - 09:18 PM

If the date is preceded by a fixed string in all filenames, and if the date in the filename is in a format that is sortablle, then all you need to do is set the Sorted property. You can do this in the Form Designer from the property list, or set it in code as follows:

lstDeliveries.Sorted = True


This will work for the following examples:

Deliveries_2013_02_26.txt (if they all start with Deliveries_)
20130226 Deliveries.txt

.. but not for the following examples:

26-02-2013 Deliveries (will sort by day, then month, then year)
2013_2_2013 Deliveries (will sort after sept, Oct, Nov. Dec)

What is the format of the file names?
Was This Post Helpful? 1
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Sorting multiple text files in order of date/time

Posted 28 August 2013 - 09:30 PM

You need do some thinking and planning before coding.

Spotted a few issues.
Why you using textfiles?
The are laws that govern how you store peoples information and data. Eg The Data Protection Act.

Assuming that the textfile exists.
Assuming that it contains valid data.

Once you've read in the correct number of lines.
Validate it.
Then put it to it's own object instance.

It a lot easier if you encapsulate the information into its own object class(es).

.ContactDetails
  .FirstName : String
  .Surname   : String
  .Address   : AddressInfo
  .ContactNo : ContactNumber
.Order
  .Requester : ContactDetails
  .Date      : DateTime
  .Items     : List(Of OrderedItem)

.OrderedItem
  .RefID    : RefID
  .Quantity : Integer
  .Name     : String



etc.




lar3ry it's better to fix the bad design at this early stage rather compound it.

This post has been edited by AdamSpeight2008: 28 August 2013 - 09:32 PM

Was This Post Helpful? 0
  • +
  • -

#6 qgzn9858  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-August 13

Re: Sorting multiple text files in order of date/time

Posted 28 August 2013 - 09:51 PM

at the moment the format of the file names are just order1.txt, order2.txt, order3.txt, etc. if i change the names to the date/time ordered is there a quick method to rename them to the date/time format that is saved on the particular line?

sorry, i should have mentioned earlier that i am a student in year 12 and am still learning VB and this is one of the tasks i am currently doing and it is not using real peoples information for a in real life use.
Was This Post Helpful? 0
  • +
  • -

#7 qgzn9858  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-August 13

Re: Sorting multiple text files in order of date/time

Posted 28 August 2013 - 11:05 PM

Thanks for the advice lar3ry, thats what i used and it worked perfectly, thanks!
i dont know why i didnt think of that.

and yes, i do realise that if this was using real peoples information txt files unencrypted it is not the safest way to store customer information, but as this is only a school task text files work fine for what i need.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1