CopyFile

Submitted by:Jhon Smith

Date added:09 November, 2017

Category:Delphi

Copies Source file to Dest file, preserving modification date.
Required units: Classes, SysUtils.

Tags: classes , dest file , sysutils

Code Snippet:

procedure CopyFile(const Source, Dest: string);
var
SourceStream, DestStream: Classes.TFileStream; // source and dest file streams
begin
DestStream := nil;
// Open source and dest file streams
SourceStream := Classes.TFileStream.Create(
Source, SysUtils.fmOpenRead or SysUtils.fmShareDenyWrite
);
try
DestStream := Classes.TFileStream.Create(
Dest, Classes.fmCreate or SysUtils.fmShareExclusive
);
// Copy file from source to dest
DestStream.CopyFrom(SourceStream, SourceStream.Size);
// Set dest file's modification date to same as source file
SysUtils.FileSetDate(
DestStream.Handle, SysUtils.FileGetDate(SourceStream.Handle)
);
finally
// Close files
DestStream.Free;
SourceStream.Free;
end;
end;
 
 

Comments