[RESOLVED]Deleting a line in a text file that ends with a certain string
Hey,
I need help deleting a line from a text file. This is for an ASP.net Webpage. I will have a file called Sources.txt. Example data of this file looks like this:
John|[email protected]
Bob|[email protected]
I would like to delete the line ending in |[email protected] programmatically. This will obviously be dynamic. I want to do this by determining what line number it is at, unless of course, there is a better way.
I want to search for the line by the ending of it so people cannot put [email protected]|[email protected] and accidentally get [email protected] deleted.
This is for an unsubscribe link.
Code:
Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt"))
lines.RemoveAt(1)
IO.File.WriteAllLines("Sources.txt", lines.ToArray())
'Stop people from refreshing and deleting other records.
Session.Clear()
End Sub
Thanks
Re: Deleting a line in a text file that ends with a certain string
Have you looked at String.EndsWith ? :)
Re: Deleting a line in a text file that ends with a certain string
Hi,
This will not work with a list. Instead I shall try using List.Find, letting you know how I get on.
Re: Deleting a line in a text file that ends with a certain string
Ok, I have found I can get it to work. But I need help
Code:
Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt"))
Dim DataList As String = lines.ToString
If DataList.EndsWith("|[email protected]") Then
lines.....
End If
I want to go through the entry and anything matching If DataList.EndsWith("|[email protected]"), I want to get the Index and then delete it with
I then want to keep going through it so I scan the whole document.
Re: Deleting a line in a text file that ends with a certain string
Why don't you go trough each line and if that line doesnt endswith that string, just add the string to a new list?
Re: Deleting a line in a text file that ends with a certain string
Hey,
This seems a little RAM inefficient, but it seems to be the only way, I tried to do an If String.Endswith = .... then GetIndexOf but it failed.
Can you give me some code to suggest this method of yours? Baring in mind after removal I was planning to recreate the file with
vb Code:
IO.File.WriteAllLines("Sources.txt", lines.ToArray())
and that there may be several R/Ws going on at once.
Re: Deleting a line in a text file that ends with a certain string
Try this:
VB.NET Code:
Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt"))
Dim finalLines As New List(Of String)
For Each line As String In lines
finalLines.Add(line)
End If
Next
Good luck
Re: Deleting a line in a text file that ends with a certain string
Will try after a 10 minute break :)
Re: Deleting a line in a text file that ends with a certain string
Perfect my good man, will give you rep and mark as resolved. I added this: IO.File.WriteAllLines("Sources.txt", finalLines.ToArray()) so that I write the final lines to the TXT file.
Code:
Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt"))
Dim finalLines As New List(Of String)
For Each line As String In lines
If Not line.EndsWith("|[email protected]") Then
finalLines.Add(line)
End If
Next
IO.File.WriteAllLines("Sources.txt", finalLines.ToArray())
End Sub