Indigo Rose Software

Professional Software Development Tools

 
+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    2

    Replacing variable in txt file

    Hi guys,

    Im getting myself totally confused trying to replace specific variables in a .txt file.

    I want to replace various parts of a txt file with session variables entered by the user.

    So first I read the text file to a string:
    Code:
    Downloads = TextFile.ReadToString(SessionVar.Get("%AppFolder%\\downloads.txt"));
    Then I get the different variables entered by the user:

    Code:
    YourName = SessionVar.Get("%yourname%");
    WebsiteName = SessionVar.Get("%websitename%");
    PayPal = SessionVar.Get("%paypalemail%");
    ProductName = SessionVar.Get("%productname%");
    Amount = SessionVar.Get("%amount%");
    Then I use string replace, to search the Downloads string, for all occourences of ##websitename##, and to replace them with the "WebsiteName" variable.
    Code:
    Thewebsitename = String.Replace("Downloads", "##websitename##", "WebsiteName", false);
    Is that correct, or am I way of course here? Also if I am on track, Im not sure of the proper method to re-search the new "TheWebsitename" string for all the other variables to replace? does it need to be saved first? Or is there a simpler method I could use?

    Any pointers would be great!

  2. #2
    Join Date
    Jan 2001
    Location
    Anderson Island, WA, USA
    Posts
    2,863
    You don't quote your variables; and when you combine a session var with text ("%AppFolder%\\Downloads.txt") you use .Expand not .Get

    Code:
    cDownloads = TextFile.ReadToString(SessionVar.Expand("%AppFolder%\\downloads.txt"));
    cWebsiteName = SessionVar.Get("%websitename%");
    cYourName = SessionVar.Get("%yourname%");
    cDownloads= String.Replace(cDownloads, "##websitename##", cWebsiteName, false);
    cDownloads= String.Replace(cDownloads, "##yourname##", cYourName, false);
    Or you can do this:
    Code:
    cDownloads = TextFile.ReadToString(SessionVar.Expand("%AppFolder%\\downloads.txt"));
    cDownloads= String.Replace(cDownloads, "##websitename##", SessionVar.Get("%websitename%"), false);
    cDownloads= String.Replace(cDownloads, "##yourname##", SessionVar.Get("%yourname%"), false);
    Last edited by jassing; 07-26-2009 at 06:04 PM.


    (Click here to contact me)
    Providing Independent Professional Consulting Services for
    IndigoRose products, World Wide.
    Located in -8:00 (-7:00 DST) GMT Timezone (Western United States)

  3. #3
    Join Date
    Feb 2009
    Posts
    2
    Hi Jassing,

    Thanks for that! It makes more sense when you see someone else write it!

    So if cDownloads string now contains the modified text, I obviously want to save it to the txt file downloads.txt

    When i try using the actions, I get the result:
    Code:
    TextFile.WriteFromString(SessionVar.Expand("%AppFolder%\\downloads.txt"), "cDownloads", false);
    but that doesn't seem right? Is that syntax correct?

    many thanks again

  4. #4
    Join Date
    Jan 2001
    Location
    Anderson Island, WA, USA
    Posts
    2,863
    "teach a man to fish; you feed him for a lifetime"

    Do not quote your lua variables.

    "give the man a fish, you feed him for a day"

    Code:
    TextFile.WriteFromString(SessionVar.Expand("%AppFolder%\\downloads.txt"),
    cDownloads, false);


    (Click here to contact me)
    Providing Independent Professional Consulting Services for
    IndigoRose products, World Wide.
    Located in -8:00 (-7:00 DST) GMT Timezone (Western United States)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts