提出问题提出问题
 

已答复Add text in middle of file

  • 2009年10月29日 8:18Uma Reddy 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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

答案

  • 2009年10月30日 4:52jwavila 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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
  • 2009年10月30日 14:10Frank L. Smith 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码

    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! :)

全部回复

  • 2009年10月30日 4:52jwavila 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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
  • 2009年10月30日 14:10Frank L. Smith 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码

    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! :)
  • 2009年11月5日 20:01Uma Reddy 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Thanks for your help. I implemented Frank's one.
  • 2009年11月5日 20:03Uma Reddy 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Thanks Frank.
    When I was reading your answer, I remembered the scene from The Ten Commandments, Parting of the Red Sea :-)


  • 2009年11月5日 22:19Frank L. Smith 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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 ...

    :)