Announcement

Collapse
No announcement yet.

Replace multiple different strings with one read/write?

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

  • Replace multiple different strings with one read/write?

    Hello IndigoRose,

    I use this code to enter data into a text file. It works perfectly fine, I'm wondering if there is a better way when multiple unique strings need to be replaced? Currently I open/read and write for each different string which requires me to open and write to the file multiple times to update the file. Maybe not very efficient. Is there a way to search and replace many different strings with only one open/read and write to the file?

    Thanks in advance.

    Code:
    ---- Read the contents.
    contentsXXXXXXX = TextFile.ReadToString(sAppFolder.."\\application.properties");
    if (error ~= 0) then
    Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
    SetupData.WriteToLogFile("ERROR: application.properties" .. Application.GetLastError(), true);
    else
    -- Replace string.
    new_contentsXXXXXXX = String.Replace(contentsXXXXXXX, "abcdef, "uvwxyz"..sAppFolder, true);
    -- Write new contents.
    TextFile.WriteFromString(sAppFolder.."\\application.properties", new_contentsXXXXXXX, false);
    err = Application.GetLastError();if err ~= 0 then sMessage = "###############\r\n" .. "ERROR " .. err .. ": " .. _tblErrorMessages[err] .. "\r\n###############\r\n"; SetupData.WriteToLogFile(sMessage, false);end
    end

  • #2
    Unless I misunderstood the question, this seems pretty obivous to me.
    Code:
    TextFile.ReadToString ();
    String.Replace();
    String.Replace();
    (...)
    TextFile.WriteFromString();
    Ulrich

    Comment


    • #3
      You understood correct. I thought of that but didn't make sense how it was accomplished. Doesn't each String.Replace needs its own unique variable or repeat the same one? Then TextFile.WriteFromString would use which variable? I'll try out some testing.
      Thanks Ulrich.
      Code:
      TextFile.ReadToString()
      new_contents = String.Replace()
      new_contents = String.Replace()
      TextFile.WriteFromString(new_contents)

      Comment


      • #4
        Think about this like so:
        Code:
        sString = TextFile.ReadToString(); -- read string
        sString = String.Replace(sString, …); -- modify
        sString = String.Replace(sString, …); -- modify further
        TextFile.WriteFromString(sString); -- save result
        Ulrich

        Comment


        • #5
          Well, now that you put it that way it makes total sense.
          Thanks again.

          Comment

          Working...
          X