PDA

View Full Version : How to write a specific word in specific place in a text file



shaitan
06-27-2006, 10:15 AM
Hi,

assume that I have a text file that it contains lines as below:


------123.txt------
destination=
name=
company=
outlog=
readonly=
update=
.
.
.
--------End--------

how can I write specific words (that stored in AMS variables after some operations) exactly after specific lines after '=' sign ?

for example assume an autorun that user can specify the "destination install path", "name", "company" &...
and after clicking a button above phrases will be saved into defined variables in autorun,
now for further operations I need to write saved variables extactly after their phrase and '=' sign in each line in specific text document.

I cannot write them in the line after the phrase (for example):

destination=
c:\test

Is there a way I can reach my target with AMS 6.0 ?

Pre Thanks

TJS
06-27-2006, 10:42 AM
If you control the format of your text file, you might want to consider using an INI file instead. AMS has built in actions that will take care of finding and replacing data in INI files (search for INI in the help file).

If you cannot change the format you can accomplish what you describe by:

1) Reading the text file to a table
2) Stepping through the table row-by-row searching for known part of the string (the part before the equal sign)
3) Remove the row where you find a match
4) Insert a row where you deleted the match with the new value

yosik
06-27-2006, 02:52 PM
I think I have a solution for you, albeit not very elegant.

The winner action is String.Replace.

Based on your example, if your text is:
------123.txt------
destination=
name=
company=
outlog=
readonly=
update=
.
.
.
--------End--------

What you could do is the following:

myContent = TextFile.ReadToString("123.txt");
String.Replace(myContent, "destination=", "destination=C://MyDocuments", false);
String.Replace(myContent, "name=", "name=John Kennedy", false);
String.Replace(myContent, "company=", "company=CIA", false);
String.Replace(myContent, "outlog=", "outlog=noIdea", false);
String.Replace(myContent, "readonly=", "readonly=yes", false);

etc...

Hope that helps

Yossi

shaitan
06-28-2006, 06:32 AM
Thanks alot TJS & yosik for your answers,
actually my text format can be replaced by ini file,
and second way is practical too.

I will use them in my autorun.
Thanks