Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have below code to modify a text file. I can find all lines that contains a specific word. but not sure how to edit that.

VB
Dim file As System.IO.File
        Dim reader As System.IO.StreamReader
        Dim line As String

        reader = file.OpenText("C:\Users\Shafiul Alam\Desktop\Test.txt")

        While reader.Peek <> -1

            line = reader.ReadLine()

            If line.Contains("DocCopies*") Then
                ' <RunList Run="Asset1" DocCopies="116" EndOfDocument="true">
                ' Get the Value of DocCopies
                ' Add +2 with 116
                ' Save the file
            Else
            End If

        End While

        reader.Close()
Posted

1 solution

You have to
  • Read the file in memory.
  • modify memory content.
  • write back modified memory content to the original file (overwriting it).


You may also produce the output 'on the fly', that is copying input to the output until you find the value that must be changed, then write the changed values and repeat such steps until the end of the file. At the end you have to overwrite the input file with the output one content.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900