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
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as