[fpc-pascal] File copying

Matt Emson memsom at interalpha.co.uk
Mon May 3 02:03:11 CEST 2004


> Any suggestions on a better method?

uses
  classes, sysutils;

....

procedure CopyFile(filename, newfilename: string);
var
  oldfile, newfile: TFilestream;
begin
  oldfile := TFileStream.Create(filename, fmOpenRead); //might need
fmShareXXX style stuff too
  try
    oldfile.Position := 0; //could use TStream.Seek(..) here also..
    newfile := TFileStream.Create(newfilename, fmCreate);
    try
      newfile.CopyFrom( oldfile, oldfile.size );
    finally
      newfile.free; // be careful.. file not actually closed till this
point...
    end;
  finally
    oldfile.free; //this closes the file..
  end;
end;


No legacy Assign[File] (or rewrite etc) in sight too.

Matt





More information about the fpc-pascal mailing list