function FileCopy(OldFile, NewFile: string): boolean;
const MaxBufSize = $F000;
var H1, H2, Count: integer;
Buffer: pChar;
begin
Result:=false;
H1:=FileOpen(OldFile, fmShareDenyNone);
if H1<0 then
Exit;
H2:=FileCreate(NewFile);
if H2<0 then
begin
FileClose(H1);
Exit;
end;
Result:=true;
{ Процесс копирования }
FileSeek(H1, 0, 0);
GetMem(Buffer, MaxBufSize);
try
repeat
Count:=FileRead(H1, Buffer^, MaxBufSize);
if Count>0 then
FileWrite(H2, Buffer^, Count);
until Count<=0;
finally
FreeMem(Buffer, MaxBufSize);
FileClose(H1);
FileClose(H2);
end;
{ Копирование атрибутов (свойств) файла }
if FileExists(NewFile) then
begin
H1:=FileAge(OldFile);
FileSetDate(NewFile, H1);
H1:=FileGetAttr(OldFile);
FileSetAttr(NewFile, H1);
end;
end;
где:
OldFile – старое имя файла (исходный файл);
NewFile – новое имя файла (конечный файл).
Например,
FileCopy('E:\123.txt', 'E:\tmp\456.txt')
выведет true,
если файл скопирован успешно; или
false, если файл
не удалось скопировать (исходный файл отсутствует, доступ запрещен, нет конечной
папки или другие причины).
|