Mega Code Archive

 
Categories / Delphi / Examples
 

Streamcopy

COPYING FILES Copy a file using a File Stream Procedure FileCopy( Const sourcefilename, targetfilename: String ); Var S, T: TFileStream; Begin S := TFileStream.Create( sourcefilename, fmOpenRead ); try T := TFileStream.Create( targetfilename, fmOpenWrite or fmCreate ); try T.CopyFrom(S, S.Size ) ; finally T.Free; end; finally S.Free; end; End; ********************************************** OR Copy a file using a buffer procedure FileCopy(const FromFile, ToFile: string); var FromF, ToF: file; NumRead, NumWritten: Word; Buf: array[1..2048] of Char; begin AssignFile(FromF, FromFile); Reset(FromF, 1); { Record size = 1 } AssignFile(ToF, ToFile); { Open output file } Rewrite(ToF, 1); { Record size = 1 } repeat BlockRead(FromF, Buf, SizeOf(Buf), NumRead); BlockWrite(ToF, Buf, NumRead, NumWritten); until (NumRead = 0) or (NumWritten <> NumRead); CloseFile(FromF); CloseFile(ToF); end; ************************************************* Hope this helps. Richard