edit text in file using streamreader and string.replace

M

moondaddy

I need to edit the text in many files so I'm writing a small routine to do
this.
First I have a method that loops through all the files in a directory and
passes the full file path to another method (ReadFile).
I'm going to use this to edit the file paths in all of my WMP play lists.
When I run this the string variable is not being changed using the replace
method. Here's what I have:


Private Sub ReadFile(ByVal path As String)
Try
Dim sr As StreamReader = New StreamReader(path)
Dim line As String
Dim sFind As String = "F:\Music"
Dim sReplace As String = "G:\Music"
Do
line = sr.ReadLine()
Debug.Print(line)
line.Replace(sFind, sReplace)
'text was not replaced
Debug.Print(line)
Loop Until line Is Nothing
sr.Close()
Catch ex As Exception
ErrLog(ex)
End Try
End Sub

What would you recomend for a good way to update and save the file with the
new text?

Thanks.
 
C

Chris Dunaway

moondaddy said:
When I run this the string variable is not being changed using the replace
method. Here's what I have:

Private Sub ReadFile(ByVal path As String)
Try
Dim sr As StreamReader = New StreamReader(path)
Dim line As String
Dim sFind As String = "F:\Music"
Dim sReplace As String = "G:\Music"
Do
line = sr.ReadLine()
Debug.Print(line)
line.Replace(sFind, sReplace)
'text was not replaced

string.Replace does not change the string, it returns a *new* string so
you must assign the result back to the variable:

line = line.Replace(sFind, sReplace)
Debug.Print(line)
Loop Until line Is Nothing
sr.Close()
Catch ex As Exception
ErrLog(ex)
End Try
End Sub

What would you recomend for a good way to update and save the file with the
new text?

You will have to read the contents of the file, make your changes and
write it out to a temporary file. Then after all the changes are
complete, delete the original file and rename the temporary file.

If the changes you make will not change the length of the file, then
you might be able to seek to the desired location in the file and just
replace the text at that point.

Chris
 
S

Stephany Young

This concept is known as that classic father-son update and was/is used
extensively in COBOL batch processing scenarios. In view this concept is one
of the first concepts that any program using any coding language shoul learn
or be taught.

As it applies to VB.NET, the first clue is that you are using a StreamReader
object. The type of the object indicates that it is used for READING and so
I am at a bit of a loss as to why you think it should be WRITING the
modified values back to the file.

The father-son update technique comprises reading the existing file, making
the required changes and writing the content, that may or may not have been
changed, to a temporary file. At the end of the process, the original file
is deleted and the tempoaray file is remaned to the original file name.

At it's most basic, it looks something like this:

Private Sub MassageFile(ByVal filename As String)

Try
Dim _sr As New StreamReader(filename)
Dim _sw As New StreamWriter(Path.ChangeExtension(filename, "tmp"))
Dim _buffer As String = _sr.ReadLine()
While _buffer IsNot Nothing
_sw.WriteLine(_buffer.Replace("F:\Music", "G:\Music")
_buffer _sr.ReadLine()
End While
_sr.Close();
_sw.Close();
File.Copy(Path.ChangeExtension(filename, "tmp"), filename, True)
File.Delete(Path.ChangeExtension(filename, "tmp"))
Catch _ex As Exception
ErrLog(_ex)
End Try

End Sub

There are other techniques that allow you to achieve the same thing with
less source code. Some of those techniques rely on the ability to read the
entire file into memory and therefore are not always suitable for large
files. Whether or not some of those techniques are more efficient than
others is often a matter of opinion but, of course, some form of performance
measure should be applied to any new technique before you decide to use it
or not.

I recommend that,before you investigate other techniques, you dissect and
gain an understanding of the technique demonstrated above because it is
important that you know what is happening 'under the bonnet' (or 'under the
hood', depending upon which part of the world you come from).
 
M

moondaddy

Thanks for the info and code sample!!! It was all good and helped me solve
the problem.


Stephany Young said:
This concept is known as that classic father-son update and was/is used
extensively in COBOL batch processing scenarios. In view this concept is
one of the first concepts that any program using any coding language shoul
learn or be taught.

As it applies to VB.NET, the first clue is that you are using a
StreamReader object. The type of the object indicates that it is used for
READING and so I am at a bit of a loss as to why you think it should be
WRITING the modified values back to the file.

The father-son update technique comprises reading the existing file,
making the required changes and writing the content, that may or may not
have been changed, to a temporary file. At the end of the process, the
original file is deleted and the tempoaray file is remaned to the original
file name.

At it's most basic, it looks something like this:

Private Sub MassageFile(ByVal filename As String)

Try
Dim _sr As New StreamReader(filename)
Dim _sw As New StreamWriter(Path.ChangeExtension(filename, "tmp"))
Dim _buffer As String = _sr.ReadLine()
While _buffer IsNot Nothing
_sw.WriteLine(_buffer.Replace("F:\Music", "G:\Music")
_buffer _sr.ReadLine()
End While
_sr.Close();
_sw.Close();
File.Copy(Path.ChangeExtension(filename, "tmp"), filename, True)
File.Delete(Path.ChangeExtension(filename, "tmp"))
Catch _ex As Exception
ErrLog(_ex)
End Try

End Sub

There are other techniques that allow you to achieve the same thing with
less source code. Some of those techniques rely on the ability to read the
entire file into memory and therefore are not always suitable for large
files. Whether or not some of those techniques are more efficient than
others is often a matter of opinion but, of course, some form of
performance measure should be applied to any new technique before you
decide to use it or not.

I recommend that,before you investigate other techniques, you dissect and
gain an understanding of the technique demonstrated above because it is
important that you know what is happening 'under the bonnet' (or 'under
the hood', depending upon which part of the world you come from).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top