Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove lines of text from txt file

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
IE
I am trying to remove lines of text from a text file, the lines of text containn the word “invalid”. I am using the (InStr) that finds the lines of text but how do I remove these lines of text from the array


Here is what I have so far.
Code:
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click


        Dim arrLines() As String = File.ReadAllLines("C:\test1.txt") 

        For j As Integer = 0 To arrLines.Length - 1
            
lineIn = arrLines(j)


            If InStr(lineIn, "invalid") Then

                'Remove the line of text from the array

            End If

        Next

        File.WriteAllLines("C:\test1.txt ", arrLines)
End Sub

Thanks
 
Look at Array.Clear.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks for the tip (Array.clear), i have tried several scenarios (arrLines.clear) but with no luck. Can you please specify the exact code that i should use as my experience with arrays is limeted.

Thanks
 
Don't make it difficult for yourself.....

Read the lines in to one array, write to a second. Use the new array to rewrite your file.

C
 
Would you have smaple code of how this is done as i do not have much experience working with arrarys.

Thanks
 
I searched the net over the weekend for array examples on what I need to achieve but got no where.

I decided to go a different approach and use a list box to remove the strings, here is the code.

Code:
Dim sr As New StreamReader("C:\test.txt ")

        While (sr.Peek() > -1)

            Dim Sline as string = sr.ReadLine

            ListBox1.Items.Add(sr.ReadLine)
        End While

        If InStr(Sline, "invalid") Then

            Me.ListBox1.Items.Remove(Me.ListBox1.SelectedItem)

        End If

        sr.Close()

Although this works I would still like to se how this is done using arrays. I would appreciate any code samples of how this is done using arrays.

Thanks
 
Blimey.....hacktastic.....

Look up System.Collections.ArrayList

C
 
My head is mushed looking at arrays, i need to see the code for what i wish to achieve so i can understand how they work.

Thanks
 
Here's an example using the Array.FindAll method with a predicate function:

Code:
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
  Const TestFileName As String = "C:\test1.txt"

  Dim arrLines() As String = File.ReadAllLines(TestFileName)

  arrLines = Array.FindAll(arrLines, AddressOf LineIsValid)

  File.WriteAllLines(TestFileName, arrLines)
End Sub

Private Function LineIsValid(ByVal Line As String) As Boolean
  Return (InStr(Line, "invalid") = 0) 'True if you want to keep the line
End Function
 
While you can still use Arrays and sometimes it is better or neccessary to do so in an instance like this a Collection or StringCollection is a much better choice. There is no reason to use that ListBox method for what you were showing you wanted to do.

Here are two ways to change this to Collections:

One way I would really suggest doing it:
Code:
        Dim sReader As New StreamReader("C:\test1.txt")
        Dim CurrentLine As String
        Dim DataLines As New System.Collections.Specialized.StringCollection

        Do While sReader.EndOfStream = False
            CurrentLine = sReader.ReadLine
            If CurrentLine.Contains("invalid") = False Then
                DataLines.Add(CurrentLine)
            End If
        Loop
        sReader.Close()
        sReader = Nothing

        Dim sWriter As New StreamWriter("C:\test1.txt")

        For Each CurrentLine In DataLines
            sWriter.WriteLine(CurrentLine)
        Next
        sWriter.Close()
        sWriter = Nothing

Here is your current code with the minimum changes I would make to use a collection:
Code:
        Dim arrlines() As String = File.ReadAllLines("C:\test1.txt")
        Dim DataLines As New System.Collections.Specialized.StringCollection
        Dim CurrentLine As String

        For Each CurrentLine In arrlines
            If InStr(CurrentLine, "invalid") <= 0 Then
                DataLines.Add(CurrentLine)
            End If
        Next

        Dim WriteArray(DataLines.Count) As String
        DataLines.CopyTo(WriteArray, 0)

        File.WriteAllLines("C:\test1.txt", WriteArray)

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thank you all for the help and code, much appreciated
 
I tried the following code and it works as expected but it also adds an extra blank line at the end of the file. I know this is not a major issue but is there a way to prevent this blank line getting addedd.

Code:
 Dim arrlines() As String = File.ReadAllLines("C:\test1.txt")
        Dim DataLines As New System.Collections.Specialized.StringCollection
        Dim CurrentLine As String

        For Each CurrentLine In arrlines
            If InStr(CurrentLine, "invalid") <= 0 Then
                DataLines.Add(CurrentLine)
            End If
        Next

        Dim WriteArray(DataLines.Count) As String
        DataLines.CopyTo(WriteArray, 0)

        File.WriteAllLines("C:\test1.txt", WriteArray)

Once a gain, thank you for all the help
 
Sorry, I've gotten so use to Collections I wasn't thinking. Zero-based.

Change:
Code:
Dim WriteArray(DataLines.Count) As String
To
Code:
Dim WriteArray(DataLines.Count [RED]- 1[/RED]) As String

I feel like the old guy sitting in his rocker telling the grand kids "I remember the good old days when there truly was a standard."

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top