Hi,
Using VB.NET 2005. I am trying to create a sub which loops through each line in a text file and does a string replace on a certain word. The sub also needs to log the line number/s which have changed and write these to a log file.
All the examples I have seen so far use the following method which reads the whole file into a string variable - does the string replace and then pumps the whole lot out to the file. This has the right result but can not log the file line number/s which were changed. i.e.
Dim Fs As FileStream = New FileStream(“C:\sample.txt”), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
Dim sw As New StreamWriter(Fs)
Dim sr As New StreamReader(Fs)
Dim str As String
str = sr.ReadToEnd()
str = str.Replace(LCase("TEST"), LCase("xxx"))
Fs.Position = 0
Fs.SetLength(str.Length)
sw.Write(str)
sw.Flush()
sw.Close()
Fs.Close()
I think I need to use the sr.ReadLine() to loop through each line - see if the line contains the word I am looking for - if it does - do a string replace on that line and write the new line to the text file + log line number to file. Is this possible?
thanks
david
Using VB.NET 2005. I am trying to create a sub which loops through each line in a text file and does a string replace on a certain word. The sub also needs to log the line number/s which have changed and write these to a log file.
All the examples I have seen so far use the following method which reads the whole file into a string variable - does the string replace and then pumps the whole lot out to the file. This has the right result but can not log the file line number/s which were changed. i.e.
Dim Fs As FileStream = New FileStream(“C:\sample.txt”), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
Dim sw As New StreamWriter(Fs)
Dim sr As New StreamReader(Fs)
Dim str As String
str = sr.ReadToEnd()
str = str.Replace(LCase("TEST"), LCase("xxx"))
Fs.Position = 0
Fs.SetLength(str.Length)
sw.Write(str)
sw.Flush()
sw.Close()
Fs.Close()
I think I need to use the sr.ReadLine() to loop through each line - see if the line contains the word I am looking for - if it does - do a string replace on that line and write the new line to the text file + log line number to file. Is this possible?
thanks
david