Announcement

Collapse
No announcement yet.

Search and Replace in A Text file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Search and Replace in A Text file

    I have a list box with a button that exports the selected item data and item text to a text file. What im looking to do is if the selected item is already in the text file, either replace it or not add it at all. when i select an item it exports it but if i select that item again it just adds it to the end og the file. im kinda new to this so any help would be great.

    This is what i got so far:

    %SelectedIndex% = ListBoxObject[Serials].GetSelected
    %ItemData% = ListBoxObject[Serials].GetItemData (%SelectedIndex%)
    %ItemText% = ListBoxObject[Serials].GetItemText (%SelectedIndex%)
    TextFile.InsertLine ("c:\My Serials.txt", "%ItemText% @ %ItemData%", -1)

  • #2
    Re: Search and Replace in A Text file

    What you're doing is called appending the file.

    Simple add an IF conditional statement in conjunction with a TEXT FILE > FIND LINE action to toggle your TEXT FILE > WRITE action on or off depending on the existence or non-existence of the line to be written within your text file.

    Corey Milner
    Creative Director, Indigo Rose Software

    Comment


    • #3
      Re: Search and Replace in A Text file

      %TextFile% =TextFile.Read ("C:\My Serials")
      %FoundPos% = String.Find ("%TextFile%", "%ItemText%", 0)
      IF (%FoundPos% = 0)
      TextFile.Write ("C:\My Serials.txt", "%ItemText% @ % ItemData%")
      END IF

      Comment


      • #4
        Re: Search and Replace in A Text file

        If the stuff you want to replace is unique in the file -- it only occurs once -- and you know what the whole search string will be (tag + data), you could just load the file into a variable with File.Read, do a String.Replace on the contents of that variable to replace the string, and then write the contents back out to the file with File.Write (with "Overwrite file with new data" enabled).

        If you want to replace a whole line, and you don't know what the whole line will be (e.g. the line is something like "FoobarPath=" followed by some text that you can't predict), then do this instead:

        <ol type="1">[*]Use TextFile.FindLine to get the line number for the line you want to replace.[*]Use TextFile.DeleteLine to nuke that line.[*]Use TextFile.InsertLine to insert your replacement line there (use the same line number).[/list=a]BTW, Worm, your code wouldn't work. [img]/ubbthreads/images/icons/smile.gif[/img] String.Find returns -1 if the string isn't found, not 0. And, your TextFile.Write would replace the whole file with just "%itemtext% @ %itemdata%"...if you wanted to append, you'd need to make it "%textfile%%itemtext% @ %itemdata%" or something along those lines.
        --[[ Indigo Rose Software Developer ]]

        Comment


        • #5
          Re: Search and Replace in A Text file

          Thanks Lorne. I guess that'll teach me to actually do a sample app rather than off the cuff.

          Comment


          • #6
            Re: Search and Replace in A Text file

            Thanks For all the Help guys but i just can't seem to get it right. Could someone give me an example please. Like I said im new at this and any help is really great

            Comment


            • #7
              Re: Search and Replace in A Text file

              To make up for my bad coding example, here it is.

              Comment

              Working...
              X