...replace text in a text file?

Author: P. Below
Homepage: http://www.teamb.com

Category: Files

procedure FileReplaceString(const FileName, searchstring, replacestring: string);
var
  
fs: TFileStream;
  S: string;
begin
  
fs := TFileStream.Create(FileName, fmOpenread or fmShareDenyNone);
  try
    
SetLength(S, fs.Size);
    fs.ReadBuffer(S[1], fs.Size);
  finally
    
fs.Free;
  end;
  S  := StringReplace(S, SearchString, replaceString, [rfReplaceAll, rfIgnoreCase]);
  fs := TFileStream.Create(FileName, fmCreate);
  try
    
fs.WriteBuffer(S[1], Length(S));
  finally
    
fs.Free;
  end;
end;

 

printed from
www.swissdelphicenter.ch
developers knowledge base