Solved

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

Posted on 2010-09-21
2
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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