Forum Discussion

msalvador's avatar
msalvador
Frequent Contributor
4 years ago
Solved

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(Filen...
  • msalvador's avatar
    msalvador
    4 years ago

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