468,544 Members | 1,759 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,544 developers. It's quick & easy.

edit text in file using streamreader and string.replace

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.

--
mo*******@noemail.noemail
Nov 29 '06 #1
4 22211
moondaddy wrote:
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

Nov 29 '06 #2
Thanks! How would I seek the a desired location in the file?
"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@l39g2000cwd.googlegr oups.com...
moondaddy wrote:
>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

Nov 29 '06 #3
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).
"moondaddy" <mo*******@noemail.noemailwrote in message
news:uv****************@TK2MSFTNGP03.phx.gbl...
>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.

--
mo*******@noemail.noemail

Nov 29 '06 #4
Thanks for the info and code sample!!! It was all good and helped me solve
the problem.
"Stephany Young" <noone@localhostwrote in message
news:eH**************@TK2MSFTNGP06.phx.gbl...
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).
"moondaddy" <mo*******@noemail.noemailwrote in message
news:uv****************@TK2MSFTNGP03.phx.gbl...
>>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.

--
mo*******@noemail.noemail


Nov 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Christine | last post: by
1 post views Thread by David Dvali | last post: by
5 posts views Thread by soup_nazi | last post: by
6 posts views Thread by tomtown.net | last post: by
6 posts views Thread by Stephen | last post: by
5 posts views Thread by dm3281 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.