Try this now... I didn't test it (because I don't have the text file), but it should work though.
VB Code:
Private Function Foo(ByVal strInputFilePath As String, Optional ByVal strOutputFilePath As String = "") As Boolean Try 'Open the file & read all the contents Dim reader As IO.StreamReader = IO.File.OpenText(strInputFilePath) Dim strContent As String = reader.ReadToEnd reader.Close() 'Split the file contents by line to an array then sort it Dim seperators As Char() = {Chr(10), Chr(13)} Dim temps() As String = strContent.Split(seperators) Dim strBldr As New System.Text.StringBuilder Dim i As Integer = 0 For i = 0 To temps.GetUpperBound(0) If temps(i).Trim().Length >= 23 Then strBldr.Append(temps(i).Substring(18, 5) & temps(i) & ChrW(13)) End If Next Dim lines() As String = strBldr.ToString.Split(seperators) Array.Sort(lines) 'Write back to the file Dim outputPath As String = strInputFilePath If strOutputFilePath <> "" Then outputPath = strOutputFilePath End If Dim writer As IO.StreamWriter = IO.File.CreateText(outputPath) For i = 0 To lines.GetUpperBound(0) writer.WriteLine(lines(i).Substring(5)) Next writer.Close() Catch ex As Exception MsgBox(ex.Message) Return False End Try Return True End Function




Reply With Quote