i want to delete few lines from a big text file , normally this lines are at the starting of the text file , so it is possible that i can enter the line no from input box and the vb.net can delete that particular line number from the text file ,
say for example if enter 1-10 so it will delete the line from 1 to 10 , as i am very new in vb.net , so kindly suggest how to go further with this.
I got the below code when tried its giving me out of memory error in the application , could you please suggest what is wrong with this code.
Dim lines As New List(Of String)(IO.File.ReadAllLines("O:\ReportAll.txt"))
'Remove the line to delete, e.g.
'lines.RemoveAt(0)
lines.RemoveRange(0, 9)
IO.File.WriteAllLines("O:\ReportAll_new.txt", lines.ToArray())
is there any other alternative in which lines can be deleted , as i am having only 4 GB of RAM , kindly suggest
with regards
Hi,
Besides IO.File and IO.File namespaces, we can also use IO.StreamReader and IO.StreamWriter namespaces to read and write files and they operate text files line by line.
However, for the reason it will not read a text file into an array/list, we cannot use List.RemoveRange method to delete lines. To handle this issue, I set a new List called delLineIndex to store the index of those lines which need to be delete. When we
write lines to the new file, we ignore these marked lines.
Here is the code:
Dim delLineIndex As New List(Of Integer)
delLineIndex.Add(6)
delLineIndex.Add(7)
delLineIndex.Add(8)
Dim openStream = New StreamReader("O:\ReportAll.txt")
Dim saveStream = New StreamWriter("O:\ReportAll_new.txt")
Dim lineStr As String = ""
Dim lineIndex As Integer = 0
Do
lineStr = openStream.ReadLine()
If lineStr Is Nothing Then
Exit Do
Else
lineIndex += 1
If Not delLineIndex.Contains(lineIndex) Then
saveStream.WriteLine(lineStr)
End If
End If
Loop Until lineStr Is Nothing
openStream.Close()
saveStream.Close()
You can find it will delete line 6, 7 and 8 from the original text file. Hope it helps you.
For more information on IO.StreamReader and IO.StreamWriter namespaces, you can refer to MSDN:
solutionsdxb
Member
76 Points
131 Posts
how to delete few lines from the text file either with line number or with content
Mar 31, 2009 05:24 PM|LINK
Hi,
i want to delete few lines from a big text file , normally this lines are at the starting of the text file , so it is possible that i can enter the line no from input box and the vb.net can delete that particular line number from the text file ,
say for example if enter 1-10 so it will delete the line from 1 to 10 , as i am very new in vb.net , so kindly suggest how to go further with this.
regards
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: how to delete few lines from the text file either with line number or with content
Mar 31, 2009 05:42 PM|LINK
have a look here
http://www.codeitbetter.com/category/text-file-handling-vb-programming-code/
Contact me
solutionsdxb
Member
76 Points
131 Posts
Re: how to delete few lines from the text file either with line number or with content
Mar 31, 2009 06:52 PM|LINK
hi thanks for your reply,
I got the below code when tried its giving me out of memory error in the application , could you please suggest what is wrong with this code.
Dim lines As New List(Of String)(IO.File.ReadAllLines("O:\ReportAll.txt")) 'Remove the line to delete, e.g. 'lines.RemoveAt(0) lines.RemoveRange(0, 9) IO.File.WriteAllLines("O:\ReportAll_new.txt", lines.ToArray())solutionsdxb
Member
76 Points
131 Posts
Re: how to delete few lines from the text file either with line number or with content
Mar 31, 2009 06:56 PM|LINK
want to add , my file size is around 30 GB
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: how to delete few lines from the text file either with line number or with content
Mar 31, 2009 07:13 PM|LINK
Yes since to read a 30 GB File in memory you shud have 30 GB RAM
Contact me
solutionsdxb
Member
76 Points
131 Posts
Re: how to delete few lines from the text file either with line number or with content
Apr 01, 2009 07:08 AM|LINK
is there any other alternative in which lines can be deleted , as i am having only 4 GB of RAM , kindly suggest
with regards
Shengqing Ya...
All-Star
45968 Points
2997 Posts
Re: how to delete few lines from the text file either with line number or with content
Apr 08, 2009 08:42 AM|LINK
Hi,
Besides IO.File and IO.File namespaces, we can also use IO.StreamReader and IO.StreamWriter namespaces to read and write files and they operate text files line by line.
However, for the reason it will not read a text file into an array/list, we cannot use List.RemoveRange method to delete lines. To handle this issue, I set a new List called delLineIndex to store the index of those lines which need to be delete. When we write lines to the new file, we ignore these marked lines.
Here is the code:
You can find it will delete line 6, 7 and 8 from the original text file. Hope it helps you.
For more information on IO.StreamReader and IO.StreamWriter namespaces, you can refer to MSDN:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx
Best Regards,
Shengqing Yang
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
solutionsdxb
Member
76 Points
131 Posts
Re: how to delete few lines from the text file either with line number or with content
Apr 15, 2009 06:17 AM|LINK
great reply , thanks a lot its really very helpful.
solutionsdxb
Member
76 Points
131 Posts
Re: how to delete few lines from the text file either with line number or with content
Apr 15, 2009 06:18 AM|LINK
great reply , thanks a lot its really very helpful.