|
-
Apr 8th, 2008, 12:46 PM
#1
Thread Starter
Addicted Member
[RESOLVED] [2008] Change A Line In A Text File
One of the features of this app I am making requires that I do this.
XML is not used and CANNOT be used.
I need to find a line in a text file and remove it.
The file has a structure like this:
jjb:/dkfhs/fjds.udi
jjb:/dkfhs/sjsk.udi
jjb:/sjaok/susk.udi
Say I want to remove a specified line of the text file so the result would make the text file look like this:
jjb:/dkfhs/fjds.udi
jjb:/sjaok/susk.udi
How would I do it?
I searched the forums and found this but it does not remove my specified line.
Code:
Dim lines As New List(Of String)
'Read the entire file into a collection of lines.
lines.AddRange(IO.File.ReadAllLines(Form1.Label7.Text))
For i As Integer = 0 To lines.Count - 1
If lines(i) = "jjb:/dkfhs/sjsk.udi" Then
lines.Insert(i + 1, "")
End If
Next
'Write out the new file contents.
IO.File.WriteAllLines(Form1.Label7.Text + "seplugins\vsh.txt", lines.ToArray())
Please help, any of it is much appreciated 
Louix.
Last edited by Louix; Apr 8th, 2008 at 01:00 PM.
-
Apr 8th, 2008, 12:55 PM
#2
Re: [2008] Change A Line In A Text File
you can't "remove" a line from a textfile.
What you need to do, is read in the text file to a string, remove what you don't want from the string, and write the entire string back to the textfile.
This is what text editors do, you just don't see it happen behind the scenes.
That being said there are a few approaches to this method.
1) you could read in the textfile to a string, line by line, and omit the lines you don't want to read in WHILE you are reading in the original text file. Then simply write it back to the file.
2) read in the file in its entirety, remove the text you don't want, write the data back to the file.
-
Apr 8th, 2008, 12:58 PM
#3
Addicted Member
Re: [2008] Change A Line In A Text File
This is probably what you want
Code:
Dim lines As New List(Of String)
Dim i As Integer
'Read the entire file into a collection of lines.
lines.AddRange(IO.File.ReadAllLines("C:\Temp\TestFile.txt"))
While i < lines.Count - 1
If lines(i) = "jjb:/dkfhs/sjsk.udi" Then
lines.RemoveAt(i)
End If
i += 1
End While
'Write out the new file contents.
IO.File.WriteAllLines("C:\Temp\TestFile.txt", lines.ToArray())
If you're going to be crazy, you have to get paid for it or else you're going to be locked up. -- Hunter Thompson
-
Apr 8th, 2008, 01:16 PM
#4
Thread Starter
Addicted Member
Re: [2008] Change A Line In A Text File
Wooo!
Thanks schaefer and kleinma the function works 100% now!
Louix.
-
Apr 8th, 2008, 01:28 PM
#5
Re: [RESOLVED] [2008] Change A Line In A Text File
well there is really no need to loop like that if there is one specific line to remove from the file.
It can be done in just 4 lines of code:
Code:
'GET ALL TEXT
Dim myString As String = My.Computer.FileSystem.ReadAllText("filenamehere")
'SPECIFY WHAT WE WANT TO FILTER OUT
Dim replaceString As String = "jjb:/dkfhs/sjsk.udi" & Environment.NewLine 'NEED THE LINE RETURN AT THE END HERE OR WE WILL END UP WITH A BLANK LINE IN THE OUTPUT
'REMOVE WHAT WE DON'T WANT
myString = myString.Replace(replaceString, String.Empty)
'WRITE DATA BACK TO FILE
My.Computer.FileSystem.WriteAllText("filenamehere", myString, False)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|