Re: Remove String From File
If you want to delete a line from a text file then you have to read the contents of the file in, remove the line, then write the new contents back out. To delete a line you simply don't write it out.
Note that you cannot write to a file that is open, so you have to either read the whole file in, close it, then write it out, or else write to a temporary file untill you've read the whole file, then close it and move the temp file to overwrite the original.
Re: Remove String From File
Thanks for your help!
cdun2
Re: Remove String From File
Actually, I still need to write the line out to another file, because I need to save the record where LEN > 384 to a seperate location. The record still needs to be removed from the source file.
Re: Remove String From File
This code reads and saves the file with out line 384.
VB Code:
'Read all lines in to a string array and close the connection to the file.
Dim lines() As String = System.IO.File.ReadAllLines("Your file path")
'Declare streamWriter object and open file for write.
Dim sw As New System.IO.StreamWriter("Your file path")
Try
'Write the strings from "lines()" array to the file with out line 384.
For i As Integer = 0 To lines.GetUpperBound(0)
If i <> 383 Then
sw.WriteLine(lines(i))
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'close the file
sw.Close()
End Try
'line 384 is still avalable for use.
Dim line384 As String
line384 = lines(383)
Re: Remove String From File
Quote:
Originally Posted by VBDT
This code reads and saves the file with out line 384.
VB Code:
'Read all lines in to a string array and close the connection to the file.
Dim lines() As String = System.IO.File.ReadAllLines("Your file path")
'Declare streamWriter object and open file for write.
Dim sw As New System.IO.StreamWriter("Your file path")
Try
'Write the strings from "lines()" array to the file with out line 384.
For i As Integer = 0 To lines.GetUpperBound(0)
If i <> 383 Then
sw.WriteLine(lines(i))
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'close the file
sw.Close()
End Try
'line 384 is still avalable for use.
Dim line384 As String
line384 = lines(383)
I thought he wants to remove the lines that are longer than 384 chars and save them to another file.... Not to remove the lines beyond line 384 ;)
Re: Remove String From File
Quote:
Originally Posted by cdun2
Actually, I still need to write the line out to another file, because I need to save the record where LEN > 384 to a seperate location. The record still needs to be removed from the source file.
Still need help on this?
Re: Remove String From File
Yes, Thanks stanav you are corrct. Thanks for pointing it out ;) Here I changed the code so that it writes the lines whose Length’s are less then 384 chars in to the file, and writes the lines whose length’s are >= 384 chars in to the new file at the same time.
VB Code:
'Read all lines in to a string array and close the connection to the file.
Dim lines() As String = System.IO.File.ReadAllLines("Your file path")
'Declare streamWriter objects and open the files for write.
Dim sw As New System.IO.StreamWriter("Your file path")
Dim sw1 As New System.IO.StreamWriter("Your new file path")
Try
For Each line As String In lines
If line.Length < 384 Then
'Write the strings from "lines()" array to the file
'whose length's are les then 384 chars.
sw.WriteLine(line)
Else
'Write the strings from "lines()" array to the file
'whose length's are >= 384 chars.
sw1.WriteLine(line)
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'close the files
sw.Close()
sw1.Close()
End Try
'All lines are still avalable in the "lines()" array for use.