PDA

Ver la Versión Completa : Mover, copiar, renombrar ficheros


Neftali [Germán.Estévez]
30-06-2006, 13:04:37
¿Cómo copiar, renombrar o mover un fichero?

Este ejemplo aparece en la ayuda de Delphi3, (no es válido para Delphi 1)


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


procedure Copia_Archivo(const FileName, DestName: TFileName);
var
CopyBuffer: Pointer; { buffer for copying }
BytesCopied: Longint;
Source, Dest: Integer; { handles }
Destination: TFileName; { holder for expanded destination name }
const
ChunkSize: Longint = 8192; { copy in 8K chunks }
begin
if (ExtractFileName(FileName)='') or (ExtractFileName(DestName)='')
then exit;
Destination := ExpandFileName(DestName); { expand the destination path}
GetMem(CopyBuffer, ChunkSize); { allocate the buffer }
try
Source := FileOpen(FileName, fmShareDenyWrite); { open source file }
if Source < 0 then raise EFOpenError.CreateFmt(SFOpenError, [FileName]);
try
Dest := FileCreate(Destination); { create output file; overwrite existing }
if Dest < 0 then raise EFCreateError.CreateFmt(SFCreateError, [Destination]);
try
repeat
BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); {read chunk }
if BytesCopied > 0 then { if we read anything... }
FileWrite(Dest, CopyBuffer^, BytesCopied); { ...write chunk}
until BytesCopied < ChunkSize; { until we run out of chunks }
finally
FileClose(Dest); { close the destination file }
end;
finally
FileClose(Source); { close the source file }
end;
finally
FreeMem(CopyBuffer, ChunkSize); { free the buffer }
end;
end;