3 Replies Latest reply on Jun 24, 2007 1:37 PM by Newsgroup_User

    Replace specific text lines in a text file

    wgb14 Level 1
      Hi all,

      This post is actually the continuation of my XML problems and questions. What i am tryign to do is to replace a specific line of text in an xml file based on the values of a text cast member. But just to simplify things up i decided to do this with a text file first and then transfer what I have learnt the in the XML problem. OK, below are two handlers that I have implemented but they are both unable to do what i want:

      I have a text file called text with three lines (one, two and three). Now:

      a) Lets assume that I want to replace only number three with number five

      on mousedown
      global gFileIO

      gFileIO = new(xtra "FileIO")
      gFileIO.openFile(the moviePath & "test8.txt", 0)

      tXMLStr = gFileIO.readFile()
      currOffset = offset(three, tXMLStr) --locate the insertation point of three inside the string tXMLStr
      setPosition(gFileIO,(currOffset)) -- set position at the insertation point
      writeString(gFileIO, "five" ) --write the new string
      gFileIO.closeFile()
      gFileIO = VOID
      end


      The result that I get is as follows:

      one
      two
      tfive (there is an extra character at the beginning which is propably due to the fact that currOffset gives as a position the end of three (11) and not its beginning 7 or t)

      b) Let's assume that I want to replace the entire contents of the file with a string that I created in director and contains the updated string. In particular:

      on mousedown

      re3 = "(three)"

      global gFileIO
      gFileIO = new(xtra "FileIO")
      gFileIO.openFile(the moviePath & "test8.txt", 0)

      tXMLStr = gFileIO.readFile()
      tXMLStr3= RegExp_Replace(tXMLStr, re3, "five", "gi") --replace it with another text
      currOffset = offset(re4, tXMLStr) --locate the insertation point of three inside the string tXMLStr
      setPosition(gFileIO, 0 ) -- set position at the begining of the file
      writeString(gFileIO,tXMLStr3) --write the new string
      gFileIO.closeFile()
      gFileIO = VOID
      end

      This is the result that I am getting

      one
      two
      fivee (for some reason there is an extra character at the end)

      Please if you have any corrections on any of the two scripts that will allow me to get what (clean results) i want let me know

      Many thanks for all the great help so far
        • 1. Re: Replace specific text lines in a text file
          Level 7
          Hi wgb14

          When I update a file I usually delete (with fileIO's deleteFile, may
          have methodname wrong) it and then write a new file. AFAIK this is the
          generally accepted and advised way to go.

          Example a I find hard to see what's wrong. Your guess is probably right
          in that you'r setting the position wrong before insertion.

          Example b is as expected: the contents of the file are rewritten, but
          the new contents are less chars then the old. So some trailing chars
          remain (the last 'e' of 'three')

          HTH,
          Manno

          wgb14 wrote:
          > Hi all,
          >
          > This post is actually the continuation of my XML problems and questions. What
          > i am tryign to do is to replace a specific line of text in an xml file based on
          > the values of a text cast member. But just to simplify things up i decided to
          > do this with a text file first and then transfer what I have learnt the in the
          > XML problem. OK, below are a number of codes that I have implemented but they
          > are all unable to do what i want:
          >
          > I have a text file called text with three lines (one, two and three). Now:
          >
          > a) Lets assume that I want to replace only number three with number five
          >
          >
          > The result that I get is as follows:
          >
          > one
          > two
          > tfive (there is an extra character at the beginning which is propably
          > due to the fact that currOffset gives as a position the end of three (11) and
          > not its beginning 7 or t)
          >
          > b) Let's assume that I want to replace the entire contents of the file with a
          > string that I created in director and contains the updated string. In
          > particular:
          >
          > on mousedown
          >
          > re3 = "(three)"
          >
          > global gFileIO
          > gFileIO = new(xtra "FileIO")
          > gFileIO.openFile(the moviePath & "test8.txt", 0)
          >
          > tXMLStr = gFileIO.readFile()
          > tXMLStr3= RegExp_Replace(tXMLStr, re3, "five", "gi") --replace it with
          > another text
          > currOffset = offset(re4, tXMLStr) --locate the insertation point of three
          > inside the string tXMLStr
          > setPosition(gFileIO, 0 ) -- set position at the begining of the file
          > writeString(gFileIO,tXMLStr3) --write the new string
          > gFileIO.closeFile()
          > gFileIO = VOID
          > end
          >
          > This is the result that I am getting
          >
          > one
          > two
          > fivee (for some reason there is an extra character at the end)
          >
          > Please if you have any corrections on any of the two scripts that will allow
          > me to get what (clean results) i want let me know
          >
          > Many thanks for all the great help so far
          >
          > on mousedown
          > global gFileIO
          >
          > gFileIO = new(xtra "FileIO")
          > gFileIO.openFile(the moviePath & "test8.txt", 0)
          >
          > tXMLStr = gFileIO.readFile()
          >
          > currOffset = offset(three, tXMLStr) --locate the insertation point of three
          > inside the string tXMLStr
          >
          > setPosition(gFileIO,(currOffset)) -- set position at the insertation point
          >
          > writeString(gFileIO, "five" ) --write the new string
          >
          > gFileIO.closeFile()
          > gFileIO = VOID
          > end
          >

          --
          ----------
          Manno Bult
          http://www.aloft.nl
          • 2. Re: Replace specific text lines in a text file
            wgb14 Level 1
            I guess I must learn to think simpler!!! Thanks for your reply!!! Just for reference do you have any idea how to set the position correctly using offset?
            • 3. Re: Replace specific text lines in a text file
              Level 7
              Hi,

              not behind director now, but I would not be surprised if FileIO is
              0-based while Director is 1-based.

              So, offset returns 7 on:
              offset("director", "adobe director")

              while fileIO might consider the "d" of "director" to be the 8th char.

              guessing though...

              Manno

              wgb14 wrote:
              > I guess I must learn to think simpler!!! Thanks for your reply!!! Just for reference do you have any idea how to set the position correctly using offset?

              --
              ----------
              Manno Bult
              http://www.aloft.nl