devxlogo

Read a Tab Delimited Text File

Read a Tab Delimited Text File

This tip shows how to read from a log file using a specified format. The following code takes all the logs for the date mentioned in the date control. The log file is in the follwing format, each value separated by TAB:

Error	FormName	10-06-2006	15:43:12	1	ErrorMessageDescriptionImports Microsoft.VisualBasic.FileIOPublic Shared sbMessage As New System.Text.StringBuilder#Region "Read from Text File"    Private Sub btnTextRead_Click( _       ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles btnTextRead.Click       Dim arrMessage As New ArrayList       Dim intarrMessageCount As Integer       Try          arrMessage = ProcessUsersFile()          If arrMessage.Count > 0 Then             If arrMessage.Count = 1 Then                If InStr(arrMessage(0), "don't exist") Then                   MessageBox.Show(arrMessage(0).ToString, _                      "No File", MessageBoxButtons.OK, _                      MessageBoxIcon.Information, _                      MessageBoxDefaultButton.Button1, _                      MessageBoxOptions.DefaultDesktopOnly)                   Exit Sub                End If             End If             For intarrMessageCount = 0 To arrMessage.Count - 1                sbMessage.Append(vbCrLf & arrMessage( _                   intarrMessageCount) & vbCrLf)             Next             Dim f As New frmShowErrorMessage             f.ShowDialog()             sbMessage.Remove(0, sbMessage.Length - 1)          Else             MessageBox.Show("No logs for the specified Date", _                "No Logs", MessageBoxButtons.OK, _                MessageBoxIcon.Information, _                MessageBoxDefaultButton.Button1, _                MessageBoxOptions.DefaultDesktopOnly)          End If       Catch ex As Exception          MsgBox(ex.Message)       End Try    End Sub   Private Function ProcessUsersFile() As ArrayList      Dim currentrow(5) As String      Dim strErrorType, strFormName, strDate, _         strTime, strThreadId, strErrorMessage As String      Dim strFileName As String = _          System.Windows.Forms.Application.StartupPath + _          "Error Log.txt"      Dim boolFileExists As Boolean      Dim dtEventDate As DateTime      Dim arrMessage As New ArrayList      boolFileExists = My.Computer.FileSystem.FileExists(strFileName)      If boolFileExists = False Then         arrMessage.Add("File " + strFileName + " don't exist")         Return arrMessage         Exit Function      End If      Using MyReader As New _         Microsoft.VisualBasic.FileIO.TextFieldParser(strFileName)         MyReader.TextFieldType = FileIO.FieldType.Delimited         MyReader.SetDelimiters(vbTab)         Dim index As Integer = 0         While Not MyReader.EndOfData            Try               currentrow = MyReader.ReadFields()               strErrorType = currentrow(0)               strFormName = currentrow(1)               strDate = currentrow(2)               strTime = currentrow(3)               strThreadId = currentrow(4)               strErrorMessage = currentrow(5)               dtEventDate = Convert.ToDateTime(strDate)               If dtEventDate = dtEventFromDt.Value Then                  arrMessage.Add(strErrorType & vbCrLf & _                     strFormName & vbCrLf & strDate & vbCrLf & _                     strTime & vbCrLf & strThreadId & vbCrLf & _                     strErrorMessage)               End If            Catch ex As Exception               Throw ex            End Try         End While      End Using      Return arrMessage   End Function#End Region
See also  5 Tips for Choosing the Right Medical Software for Your Practice
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist