473,390 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 23006
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Christine | last post by:
I have code right now that reads in the file into an arraylist, but it reads in each line of the text file as a single element rather than the separate tab delimited strings in the line. Is there...
31
by: J.S. | last post by:
Let's say I have a text file with parameters like the following embedded in the text: @@Textbox1@@, @@Textbox2@@, etc. Is it possible to replace the parameters in the text file with values...
1
by: David Dvali | last post by:
Hello. I have a problem with sending Unicode text in mail message. So what I do: First of all I have some template file like this: ================================= <html> <head><title>Test...
5
by: soup_nazi | last post by:
I want to remove duplicate entries within a text file. So if I had this within a text file... Applications/Diabetic Registry/ Applications/Diabetic Registry/ Applications/Diabetic Registry/...
6
by: tomtown.net | last post by:
Hello I'm trying to get a single line removed from a text file using a search pattern (pretty simple: if line contains "NODE1") -> remove line). To achieve this I's like to operate with only the...
6
by: Stephen | last post by:
I am reading a text file using TextReader reader = new StreamReader("file.txt"); like I have done many a times before. I then execute the statement string a = reader.ReadToEnd(); this string is...
1
by: ollielaroo | last post by:
Hi guys, Firstly I did do a search for this one first but I couldn't find anything related in this forum. I am using Dreamweaver MX and trying to build admin pages for an ASP site. My problem is...
3
by: TonyJ | last post by:
Hello! I supposed to modify some text files. In the file(s) which consist of different section. In one section you might have Path = <Installation_path>. The program will then replace the text...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.