edit text in file using streamreader and string.replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • moondaddy

    #1

    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(pa th)
    Dim line As String
    Dim sFind As String = "F:\Music"
    Dim sReplace As String = "G:\Music"
    Do
    line = sr.ReadLine()
    Debug.Print(lin e)
    line.Replace(sF ind, sReplace)
    'text was not replaced
    Debug.Print(lin e)
    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.

    --
    moondaddy@noema il.noemail


  • Chris Dunaway

    #2
    Re: edit text in file using streamreader and string.replace

    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(pa th)
    Dim line As String
    Dim sFind As String = "F:\Music"
    Dim sReplace As String = "G:\Music"
    Do
    line = sr.ReadLine()
    Debug.Print(lin e)
    line.Replace(sF ind, 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(sF ind, sReplace)
    Debug.Print(lin e)
    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

    Comment

    • moondaddy

      #3
      Re: edit text in file using streamreader and string.replace

      Thanks! How would I seek the a desired location in the file?


      "Chris Dunaway" <dunawayc@gmail .comwrote in message
      news:1164826285 .792095.175870@ l39g2000cwd.goo glegroups.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(pa th)
      > Dim line As String
      > Dim sFind As String = "F:\Music"
      > Dim sReplace As String = "G:\Music"
      > Do
      > line = sr.ReadLine()
      > Debug.Print(lin e)
      > line.Replace(sF ind, 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(sF ind, sReplace)
      >
      > Debug.Print(lin e)
      > 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
      >

      Comment

      • Stephany Young

        #4
        Re: edit text in file using streamreader and string.replace

        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(ByV al filename As String)

        Try
        Dim _sr As New StreamReader(fi lename)
        Dim _sw As New StreamWriter(Pa th.ChangeExtens ion(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(Pat h.ChangeExtensi on(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" <moondaddy@noem ail.noemailwrot e in message
        news:uvg7GH%23E HHA.1248@TK2MSF TNGP03.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(pa th)
        Dim line As String
        Dim sFind As String = "F:\Music"
        Dim sReplace As String = "G:\Music"
        Do
        line = sr.ReadLine()
        Debug.Print(lin e)
        line.Replace(sF ind, sReplace)
        'text was not replaced
        Debug.Print(lin e)
        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.
        >
        --
        moondaddy@noema il.noemail
        >

        Comment

        • moondaddy

          #5
          Re: edit text in file using streamreader and string.replace

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


          "Stephany Young" <noone@localhos twrote in message
          news:eH5h7HAFHH A.1188@TK2MSFTN GP06.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(ByV al filename As String)
          >
          Try
          Dim _sr As New StreamReader(fi lename)
          Dim _sw As New StreamWriter(Pa th.ChangeExtens ion(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(Pat h.ChangeExtensi on(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" <moondaddy@noem ail.noemailwrot e in message
          news:uvg7GH%23E HHA.1248@TK2MSF TNGP03.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(pa th)
          > Dim line As String
          > Dim sFind As String = "F:\Music"
          > Dim sReplace As String = "G:\Music"
          > Do
          > line = sr.ReadLine()
          > Debug.Print(lin e)
          > line.Replace(sF ind, sReplace)
          >'text was not replaced
          > Debug.Print(lin e)
          > 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.
          >>
          >--
          >moondaddy@noema il.noemail
          >>
          >
          >

          Comment

          Working...