Solved

Best Method To Remove Lines In Text File Using Visual Basic Or C#

Posted on 2010-09-21
2
361 Views
Last Modified: 2013-12-16
I've got code that works spectacularly to parse a text file (Thanks Idle_Mind!!).  However, the text file has thrown a curve into the mix.  I'll put the link to the original post below.

The text file includes lines with a missing second column.  The resulting text file contains lines that begin with commas.  I require those lines that begin with commas removed from the final text files.

My first thought was to open the text files after they have been created, then look for lines that start with commas and remove them.  However, I realized there might be an easier way that I do not know.

http://www.experts-exchange.com/Programming/Languages/.NET/Q_26472802.html hotlist.txt  
Option Explicit On

Imports System
Imports System.IO
Imports System.IO.File
'##############################
'REQUIREMENTS:
'Application runs invisibly
'There are 11 colomns, each represented by white space which is the start/end of each column
'Keep only those rows that contain the text "OH" in the 3rd column
'Keep only the following columns: 2, 3, 6, 7, 8, 9, and 11
'Output data to text files as comma sperated based on first character in column 11:
'   if column 11 starts with "V", create a file called "Stolen.txt"
'   if column 11 starts with "W", create a file called "Wanted.txt"
'   if column 11 starts with "P", create a file called "LP.txt"
'   if column 11 starts with "M", create a file called "MP.txt"

'##############################
Public Class DOJ_Parser_Hilliard
    'Global variables
    Public strFileName As String = "hotlist.txt"
    Public strCurrent As String = Directory.GetCurrentDirectory()
    Public strGlobalFilePath As String = strCurrent & "\" & strFileName
    'LOAD Form, check for file existance
    Private Sub DOJ_Parser_Hilliard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Label1.Text = "Loading . . ."

        If VerifyFileExists() = True Then
            ParseMe()
            'test()
        Else
            MessageBox.Show("File not found.", "WARNING!")
            Me.Close()
        End If
        Me.Close()
    End Sub
    'PARSE DATA and write to files
    Private Sub ParseMe()

        MessageBoxTimer(500)
        MessageBox.Show("Data Extraction Commencing...", "PROCESSING")
        Dim Path As String = strCurrent
        Dim DataFile As String = strFileName
        Dim V_File As String = "Stolen.txt"
        Dim W_File As String = "Wanted.txt"
        Dim LP_File As String = "LP.txt"
        Dim MP_File As String = "MP.txt"

        Using sw_V As New StreamWriter(System.IO.Path.Combine(Path, V_File), False)
            Using sw_W As New StreamWriter(System.IO.Path.Combine(Path, W_File), False)
                Using sw_LP As New StreamWriter(System.IO.Path.Combine(Path, LP_File), False)
                    Using sw_MP As New StreamWriter(System.IO.Path.Combine(Path, MP_File), False)

                        Using sr As New StreamReader(System.IO.Path.Combine(Path, DataFile))
                            While Not sr.EndOfStream
                                Dim values As New List(Of String)
                                values.AddRange(sr.ReadLine.Split(" "))
                                If values(2).ToUpper = "0H" Then ' process lines with "OH" in the 3rd column
                                    ' remove columns indexed at: 0, 3, 4, 9
                                    values.RemoveAt(9)
                                    values.RemoveAt(4)
                                    values.RemoveAt(3)
                                    values.RemoveAt(0)
                                    Dim output As String = String.Join(",", values.ToArray)
                                    ' output to file based on first character of value in last colmun
                                    Select Case values(values.Count - 1).Substring(0, 1).ToUpper
                                        Case "V"
                                            sw_V.WriteLine(output)
                                        Case "W"
                                            sw_W.WriteLine(output)
                                        Case "P"
                                            sw_LP.WriteLine(output)
                                        Case "M"
                                            sw_MP.WriteLine(output)
                                    End Select
                                End If
                            End While
                        End Using

                    End Using
                End Using
            End Using
        End Using
    End Sub
    'VERIFY File Exists
    Private Function VerifyFileExists()
        'this function runs on form load and tries to find the specified file
        Try
            strCurrent = Directory.GetCurrentDirectory()
        Catch E As Exception
            MessageBox.Show(E.Message)
        End Try
        If Not Exists(strGlobalFilePath) Then
            MessageBox.Show("File Not Found.", "WARNING!")
            Return False
        Else
            Return True
        End If
    End Function
    'TIMER to control MessageBox
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Enabled = False  'end timer
        SendKeys.Send("{ESC}")  'send escape key press to messagebox
    End Sub
    'TIMER set length and start timer
    Private Sub MessageBoxTimer(ByVal intervalLength As Integer)
        'set length of time and start timer
        Timer1.Interval = intervalLength
        Timer1.Enabled = True
    End Sub

    'open a stream reader and remove lines that start with comma
    Private Sub test()
        Try
            ' Create an instance of StreamReader to read from a file.
            Dim sr As StreamReader = New StreamReader(strCurrent & "\Wanted.txt")
            Dim line As String
            ' Read and display the lines from the file until the end 
            ' of the file is reached.
            Do
                line = sr.ReadLine()

                'If line.Substring(0, 0) = "," Then  
                'MessageBox.Show(line)
                'Remove(2, line.Length - 1)
                'End If
            Loop Until line Is Nothing
            sr.Close()
        Catch E As Exception
            ' Let the user know what went wrong.
            MessageBox.Show("The file could not be read:")
            MessageBox.Show(E.Message)
        End Try
    End Sub
End Class

Open in new window

0
Comment
Question by:asc2010
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33728603
Not sure I'm following 100% but it seems to me that you could simply change:

    If values(2).ToUpper = "0H" Then

To:

    If values(2).ToUpper = "0H" AndAlso values(1).Trim <> "" Then

This will only allow lines that are NOT blank in the second column AND have "OH" in the third column to pass thru and be processed.
0
 

Author Closing Comment

by:asc2010
ID: 33728980
Idle_Mind,

Thank you so much!!  This is perfect and much more efficient than my approach!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now