Results 1 to 7 of 7

Thread: Sorting a txt file with 1st 12 char as keys

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    5

    Sorting a txt file with 1st 12 char as keys

    OK, this VB 2003 is way different than programming in "C" 15 years ago.

    Now a lawyer, I'm programming to pass time. I tried IO.File.ReadAllLines, however it's not in ver 2003.

    This first 12 characters of a record in my txt file is what I want to sort on. A typical record starts like this "01E118750216. . ." all ASCII with a CRLF to end the record. The file could be big with 2000 records.

    Russell

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Sorting a txt file with 1st 12 char as keys

    Thread Moved from VB FAQ Forum
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Sorting a txt file with 1st 12 char as keys

    Are the first 12 character unique?
    If programming .NET is a hobby for you, check out latest express versions: http://www.microsoft.com/express/

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Sorting a txt file with 1st 12 char as keys

    Quote Originally Posted by wild_bill
    Are the first 12 character unique?
    I guess it doesn't matter since vb7 doesn't include a sorted dictionary object
    vb Code:
    1. Dim data As New DataTable
    2. data.Columns.Add("key")
    3. data.Columns.Add("value")
    4.  
    5. Dim sr As New IO.StreamReader("c:\test.txt")
    6. Dim line As String = sr.ReadLine
    7. While Not line Is Nothing
    8.     data.Rows.Add(New String() {line.Substring(0, 12), line.Substring(12)})
    9.     line = sr.ReadLine
    10. End While
    11.  
    12. sr.Close()
    13.  
    14. For Each dr As DataRow In data.Select("", "key")
    15.     'do what ever with the sorted results
    16. Next

    The next time I'm arrested for exposing myself in public, I'll know who to call.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    5

    Re: Sorting a txt file with 1st 12 char as keys

    Sorry.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Sorting a txt file with 1st 12 char as keys

    vb.net Code:
    1. Dim reader As New IO.StreamReader("file path here")
    2. Dim fileContents As String = reader.ReadToEnd()
    3.  
    4. reader.Close()
    5.  
    6. Dim lines As String() = System.Text.RegularExpressions.Regex.Split(fileContents, Environment.NewLine)
    7. Dim upperBound As Integer = lines.GetUpperBound(0)
    8. Dim keys(upperBound) As String
    9. Dim line As String
    10.  
    11. For index As Integer = 0 To upperBound Step 1
    12.     line = lines(index)
    13.     keys(index) = line.Substring(0, Math.Min(12, line.Length))
    14. Next index
    15.  
    16. Array.Sort(keys, lines)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    5

    Re: Sorting a txt file with 1st 12 char as keys

    Howdy,

    I wish to THANK everyone for their posts. I didn't follow exactly what y'all (I'm from the Republic of Texas) suggested. However, it pointed me in the right direction with the right subroutines. Now, I'm pretty busy at work and have litle time to devote to my "pet" projects. The following code is what I wrote from y'alls(Texan draw) suggestions. Not as eloquent or efficient, but functional. I like functional. Basically, I read my txt data into a string array, Parse the array into two other arrays, Sort, rejoin the the orignal sorted arrays and write it.

    Private Sub SortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortButton.Click
    Dim data As New DataTable
    Dim clTodayDate As Date = Date.Now
    Dim iTodayMonth As Integer = clTodayDate.Month
    Dim ThisMonthStreamReader As StreamReader
    Dim stDayEventLine As String
    Dim starEventData() As String ' includes keys and actual event
    ' "st" is a String and "ar" in an Array so the prefix "star" is a string array
    Dim iUpperBoundIndex As Integer = 0
    Dim stTempPath As String = "c:\program files\today\temp.txt"


    Try
    ' create a temp file
    Dim swTempEvent As StreamWriter = File.CreateText(stTempPath)

    ' Open this month
    ThisMonthStreamReader = File.OpenText(GetPath(iTodayMonth))
    stDayEventLine = ThisMonthStreamReader.ReadLine()


    While Not stDayEventLine = Nothing
    starEventData(iUpperBoundIndex) = stDayEventLine
    iUpperBoundIndex += 1
    stDayEventLine = ThisMonthStreamReader.ReadLine()
    End While

    Dim starKeys(iUpperBoundIndex) As String ' Keys of the Event
    Dim starEvent(iUpperBoundIndex) As String ' Actual Event


    For index As Integer = 0 To iUpperBoundIndex Step 1
    starKeys(index) = starEventData(index).Substring(0, 11)
    starEvent(index) = starEventData(index).Substring(12)
    Next index

    Array.Sort(starKeys, starEvent)

    For index As Integer = 0 To iUpperBoundIndex Step 1
    starEventData(index) = starKeys(index) + starEvent(index)
    swTempEvent.WriteLine(starEventData(index))
    Next index

    File.Copy(stTempPath, GetPath(iTodayMonth), True)

    Catch ex As Exception
    ' Let the user know what went wrong.
    Console.WriteLine("The file could not be read:")
    Console.WriteLine(ex.Message)

    End Try

    ThisMonthStreamReader.Close()
    File.Delete(stTempPath)


    End Sub
    ______________________________

    I think this will work, however, I have not had time to test it.

    rhgarrett
    Last edited by rhgarrett; Feb 6th, 2008 at 10:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width