cancel
Showing results for 
Search instead for 
Did you mean: 

replace a string in a file

SOLVED
Highlighted
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
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Contributor

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

View solution in original post

4 REPLIES 4
Highlighted
Community Hero

For readability, repasting your function formatted better.

 

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++;
    }
    myFile.Close();
}

That said.... rather than reading the file one line at a time and then trying to replace the line, you could read the whole text file in all at once using aqFile.ReadWholeTextFile.  This will read the whole file in as a single string.  You can then simply use the aqString.Replace method to replace the desired string and then save the file back down, overwriting the existing file.

 


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Highlighted
Contributor

Ok I will try.

Highlighted
Contributor

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

View solution in original post

Community Manager

Thank you for sharing @msalvador 


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
New Here?
Join us and watch the welcome video:
Announcements
Join TechCorner Challenge!
Want a fun and easy way to learn TestComplete? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get properties of a web page element

Participate!

Compare images using the Region Checkpoint

See replies!

Compare HTML table with Excel file and correct data in Excel file

Participate!

How to execute remote test and obtain results via Test Runner REST API

Participate!
Top Kudoed Authors