From: "Peter Below (TeamB)" <100113.1101@compuXXserve.com> Subject: Re: compare files ? Date: 10 Aug 1999 00:00:00 GMT Message-ID: Content-Transfer-Encoding: 8bit References: <7omh0v$71s16@forums.borland.com> <7omt25$7ib17@forums.borland.com> <7on5gj$80f8@forums.borland.com> <7onbnc$80f20@forums.borland.com> Content-Type: text/plain; charset=iso-8859-1 Organization: TeamB Mime-Version: 1.0 Reply-To: 100113.1101@compuXXserve.com Newsgroups: borland.public.delphi.objectpascal In article <7onbnc$80f20@forums.borland.com>, Tom wrote: > I meant to search a specify string in a file and then replace it. > This is a general method to replace a sequence of byte with another one in any kind of file, untested! Procedure ReplaceInFile( const filename: String; const findbytes, replacebytes: Array of byte ); Function BytesToString( const bytes: Array of byte ): String; Begin SetLength( result, High(bytes)-Low(bytes)+1); If length(result) > 0 Then Move( bytes[0], result[1], length( result )); End; { BytesToString } Var S: String; f: TFileStream; findStr, replaceStr: String; Begin // convert array of bytes to strings findStr := BytesToString( findbytes ); replaceStr := BytesToString( replacebytes ); // open file and read it into a string f:= TFilestream.create( filename, fmOpenRead or fmShareDenyNone ); try SetLength( S, f.Size ); f.read( S[1], Length(S)); finally f.free; end; // replace first instance of byte sequence only S:= Stringreplace( S, findStr, replaceStr, []) ; // make backup copy of source file Copyfile( Pchar( filename ), PChar(Changefileext(filename, '.BAK')), false ); // write modified string f:= TFilestream.Create( filename, fmCreate ); try f.Write( S[1], Length(S)) finally f.free end; End; { ReplaceInFile } Peter Below (TeamB) 100113.1101@compuserve.com) No e-mail responses, please, unless explicitly requested!