Visual Basic > Visual Basic Forums > Visual Basic IDE > Add text in middle of file
Ask a questionAsk a question
 

AnswerAdd text in middle of file

  • Thursday, October 29, 2009 8:18 AMUma Reddy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

Answers

  • Friday, October 30, 2009 4:52 AMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Friday, October 30, 2009 2:10 PMFrank L. Smith Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

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

All Replies

  • Friday, October 30, 2009 4:52 AMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Friday, October 30, 2009 2:10 PMFrank L. Smith Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    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! :)
  • Thursday, November 05, 2009 8:01 PMUma Reddy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for your help. I implemented Frank's one.
  • Thursday, November 05, 2009 8:03 PMUma Reddy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Frank.
    When I was reading your answer, I remembered the scene from The Ten Commandments, Parting of the Red Sea :-)


  • Thursday, November 05, 2009 10:19 PMFrank L. Smith Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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 ...

    :)