PDA

View Full Version : Editin multiple lines in text file



gott
09-12-2007, 08:20 AM
Is it possible to edit more than one string within a text file using the TextFile.WriteToString command? I have numerous text files that I need to edit more than just one word within its contents. I've looked into writing the contents of the text file to a table, but it seems like a long and involved process just to edit multiple lines. Thanks.

gott
09-12-2007, 10:25 AM
To clarify, if I wanted to replace more than one string within a text file, could I achieve this by the following:



String.Replace(fileContents, "this", "that", true);
String.Replace(fileContents, "here", "there", true);


Where I am attempting to replace all occurrences of "this" with "that" and "here" with "there" (just as examples of course). Also, then I would need to store both of these changes into a variable that I would need to write back to the directory. If I am either on the right or wrong track, please let me know. Thanks.

pww
09-12-2007, 02:13 PM
this is surely possible, you need to read the file to some variable, and then sequentially perform all the desired replacements to that variable. Finally, write the modified variable to a text file(same or new). For example



myTxt = TextFile.ReadToString("C:\\test.txt");
myTxt = String.Replace(myTxt, "this", "that", true);
myTxt = String.Replace(myTxt, "here", "there", true);
myTxt = String.Replace(myTxt, "something", "anotherthing", true);
TextFile.WriteFromString("C:\\test_1.txt", myTxt, false);

jassing
09-12-2007, 06:53 PM
Be sure to pay attention to what you're changing! it is not a regex replacement....

i you replace "at" with "@" in "what is that boy standing at attention for?" you will get
"wh@ is th@ boy standing @ @tention for? "