Re: How to search for a specific string in a txt file (FindComponent?)




<pjwolters@xxxxxxxxx> wrote in message
news:1114456563.484880.239940@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Can anyone tell me how to find a specific string in a txt file.
> I need to find this string and then change it into something else. I've
> tried using the FindComponent but no success.

procedure ReplaceStringInFile (const fileName, oldStr, newStr : string);

var ms : tStringList;

begin
if FileExists (fileName)
then begin
ms := tStringList.Create;
try
ms.LoadFromFile (fileName);
ms.Text := StringReplace (ms.Text, oldStr, newStr, [rfReplaceAll,
rfIgnoreCase]);
ms.SaveToFile (fileName);
finally
ms.Free;
end;
end
else raise Exception.Create ('Cannot find file "' + fileName + '".');
end;


.



Relevant Pages