Add text in middle of file
Hi
My problem is to open a file, search for a particular word in that file, After finding that in line, add some text in the following line.
Like for example I am searching for "Serach" in a file , and I found it in line 16.
I need to add few lines of code in line 17.And the remaining lines pushes down.
Can anyone help me pls?
Thanks
答案
- well, there would be a number of ways to do this.
to begin with, a textfile does not have line indexes. So you would have to read the entire file into something like a RichTextBox or a List(Of String).
Then you can search each line (either in the RichTextBox or the List) for your term. Once you find it, you can then insert your new lines at that point.
Using a RichTextBox makes it easy to re-save the text- 已标记为答案Jeff ShanMSFT, Moderator2009年11月2日 7:47
- 已编辑jwavila 2009年10月30日 14:45
Another way that you might consider doing it would be to read the text file up to the point of the word that you're searching for, then set two string variables to hold the text up to and including that point in the file and the text following the same (effectively splitting the text file into two portions). Next, insert a line with your text and then put the whole thing back together again.
Just rereading that makes me wonder if I made any sense here, but hopefully the following might be a start for you. I put this in the button click event since I don't know just how/where you'd want it to work:Dim inputFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\History_Of_Halloween.txt" Dim outputFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\History_Of_Halloween_Revised.txt" Dim textToFind As String = "celebration" Dim startTxt As String Dim addTxt As String = "This is the line of text to add..." Dim endText As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim locateTxt As Integer = InStr(My.Computer.FileSystem.ReadAllText(inputFile), textToFind) startTxt = Microsoft.VisualBasic.Left(My.Computer.FileSystem.ReadAllText(inputFile), (locateTxt + textToFind.Length)) endText = Replace(My.Computer.FileSystem.ReadAllText(inputFile), startTxt, "") My.Computer.FileSystem.WriteAllText(outputFile, startTxt & vbCrLf & addTxt & vbCrLf & Trim(endText), False) Beep() End Sub
I have the two test files uploaded here if you're interested: http://www.fls-tech.com/VBNet_Forum/10-30-09/TestTextFiles.zip
A few caveats to mention here: If the word that you're searching for is in the text file more than once, it will only locate the first instance of it. You'd be better to set it to locate a phrase or something that's reasonably distinctive. Also, depending on how the text file is formatted and where it's split, it may end up adding extra white space (a blank line). That didn't happen here but I can see how that may could.
Anyway, hopefully this will give you fodder for thought.
Good luck! :)- 已标记为答案Jeff ShanMSFT, Moderator2009年11月2日 7:47
全部回复
- well, there would be a number of ways to do this.
to begin with, a textfile does not have line indexes. So you would have to read the entire file into something like a RichTextBox or a List(Of String).
Then you can search each line (either in the RichTextBox or the List) for your term. Once you find it, you can then insert your new lines at that point.
Using a RichTextBox makes it easy to re-save the text- 已标记为答案Jeff ShanMSFT, Moderator2009年11月2日 7:47
- 已编辑jwavila 2009年10月30日 14:45
Another way that you might consider doing it would be to read the text file up to the point of the word that you're searching for, then set two string variables to hold the text up to and including that point in the file and the text following the same (effectively splitting the text file into two portions). Next, insert a line with your text and then put the whole thing back together again.
Just rereading that makes me wonder if I made any sense here, but hopefully the following might be a start for you. I put this in the button click event since I don't know just how/where you'd want it to work:Dim inputFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\History_Of_Halloween.txt" Dim outputFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\History_Of_Halloween_Revised.txt" Dim textToFind As String = "celebration" Dim startTxt As String Dim addTxt As String = "This is the line of text to add..." Dim endText As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim locateTxt As Integer = InStr(My.Computer.FileSystem.ReadAllText(inputFile), textToFind) startTxt = Microsoft.VisualBasic.Left(My.Computer.FileSystem.ReadAllText(inputFile), (locateTxt + textToFind.Length)) endText = Replace(My.Computer.FileSystem.ReadAllText(inputFile), startTxt, "") My.Computer.FileSystem.WriteAllText(outputFile, startTxt & vbCrLf & addTxt & vbCrLf & Trim(endText), False) Beep() End Sub
I have the two test files uploaded here if you're interested: http://www.fls-tech.com/VBNet_Forum/10-30-09/TestTextFiles.zip
A few caveats to mention here: If the word that you're searching for is in the text file more than once, it will only locate the first instance of it. You'd be better to set it to locate a phrase or something that's reasonably distinctive. Also, depending on how the text file is formatted and where it's split, it may end up adding extra white space (a blank line). That didn't happen here but I can see how that may could.
Anyway, hopefully this will give you fodder for thought.
Good luck! :)- 已标记为答案Jeff ShanMSFT, Moderator2009年11月2日 7:47
- Thanks for your help. I implemented Frank's one.
- Thanks Frank.
When I was reading your answer, I remembered the scene from The Ten Commandments, Parting of the Red Sea :-) Thanks Frank.
When I was reading your answer, I remembered the scene from The Ten Commandments, Parting of the Red Sea :-)
Ha!
VB is good but ...
:)

