¿Cómo copiar, renombrar o mover un fichero?
Este ejemplo aparece en la ayuda de Delphi3, (no es válido para Delphi 1)
Código Delphi
[-]
procedure TFMForm.ConfirmChange(const ACaption, FromFile, ToFile: string);
begin
if MessageDlg(Format('%s %s to %s?', [ACaption, FromFile, ToFile]),
mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
if ACaption = 'Move' then
MoveFile(FromFile, ToFile)
else if ACaption = 'Copy' then
CopyFile(FromFile, ToFile)
else if ACaption = 'Rename' then
RenameFile(FromFile, ToFile);
FileList.Update;
end;
end;
Este codigo está en los ejemplos de Delphi3 y si es válido para Delphi 1
Código Delphi
[-]
procedure Copia_Archivo(const FileName, DestName: TFileName);
var
CopyBuffer: Pointer;
BytesCopied: Longint;
Source, Dest: Integer;
Destination: TFileName;
const
ChunkSize: Longint = 8192;
begin
if (ExtractFileName(FileName)='') or (ExtractFileName(DestName)='')
then exit;
Destination := ExpandFileName(DestName);
GetMem(CopyBuffer, ChunkSize);
try
Source := FileOpen(FileName, fmShareDenyWrite);
if Source < 0 then raise EFOpenError.CreateFmt(SFOpenError, [FileName]);
try
Dest := FileCreate(Destination);
if Dest < 0 then raise EFCreateError.CreateFmt(SFCreateError, [Destination]);
try
repeat
BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize);
if BytesCopied > 0 then
FileWrite(Dest, CopyBuffer^, BytesCopied);
until BytesCopied < ChunkSize;
finally
FileClose(Dest);
end;
finally
FileClose(Source);
end;
finally
FreeMem(CopyBuffer, ChunkSize);
end;
end;