msalvador
5 years agoFrequent Contributor
replace a string in a file
Hi guys, I need to find a string in a file and replace with another string I find: web.admin.user=admin and I want to chane with: web.admin.user=pippo a snippet: function FindandReplaceTextValue(Filename, OldStr, NewStr) //Find a text in a file and replace it { var myFile = aqFile.OpenTextFile(Filename, aqFile.faReadWrite , aqFile.ctUTF8); var Str; myFile.SetPosition(1,1); Str = myFile.ReadLine(); // Reads text lines from the file and posts them to the test log while(! myFile.IsEndOfFile()) { Str = myFile.ReadLine(); if (Str == OldStr) { myFile.Cursor--; myFile.r myFile.Write(NewStr) //Replace the string } myFile.Cursor++; } // Closes the file myFile.Close(); } this do not work because add a new line
Here the solution:
function FindandReplaceTextValue(Filename, OldStr, NewStr) //Find a text in a file and replace it
{
var sPath = Filename;
// Opens the created file for reading
var myFile = aqFile.ReadWholeTextFile(sPath, aqFile.ctANSI);
if (aqString.Find(myFile,OldStr)!= -1)
{
myFile = aqString.replace(myFile, OldStr, NewStr); //Replace string
aqFile.WriteToTextFile(Filename, myFile, aqFile.ctANSI, true);
}
}