PDA

View Full Version : Replacing variable in txt file


drillbitt
07-26-2009, 06:19 PM
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:
Downloads = TextFile.ReadToString(SessionVar.Get("%AppFolder%\\downloads.txt"));

Then I get the different variables entered by the user:

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.
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!

jassing
07-26-2009, 07:00 PM
You don't quote your variables; and when you combine a session var with text ("%AppFolder%\\Downloads.txt") you use .Expand not .Get

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:

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);

drillbitt
07-26-2009, 07:48 PM
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:
TextFile.WriteFromString(SessionVar.Expand("%AppFolder%\\downloads.txt"), "cDownloads", false);
but that doesn't seem right? Is that syntax correct?

many thanks again

jassing
07-26-2009, 08:01 PM
"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"

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