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
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.