procedure CopyFile(const FileName, DestName: string); var CopyBuffer: Pointer; { buffer for copying } BytesCopied: Longint; Source, Dest: Integer; { handles } Len: Integer; Destination: TFileName; { holder for expanded destination name } const ChunkSize: Longint = 8192; { copy in 8K chunks } begin Destination := ExpandFileName(DestName); { expand the destination path } if HasAttr(Destination, faDirectory) then { if destination is a directory... } begin Len := Length(Destination); if Destination[Len] = '\' then Destination := Destination + ExtractFileName(FileName) { ...clone file name } else Destination := Destination + '\' + ExtractFileName(FileName); { ...clone file name } end; 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; procedure TForm1.Button1Click(Sender: TObject); begin CopyFile('c:\MyDatabase\MyBase.fdb', 'c:\Sauvegarde\'); end;
procedure copier(Handle: HWND; Source, Cible: string; FlagsOptions: FILEOP_FLAGS); var lpFileOp: TSHFileOpStructA; TabFrom: array[0..255] of char; i: integer; begin for i := 0 to length(Source) - 1 do TabFrom[i] := Source[i + 1]; TabFrom[length(Source)] := #0; TabFrom[length(Source) + 1] := #0; lpFileOp.Wnd := handle; lpFileOp.wFunc := FO_COPY; lpFileOp.pFrom := TabFrom; lpFileOp.pTo := PAnsiChar(Cible); lpFileOp.fFlags := FlagsOptions; SHFileOperation(lpFileOp); end;
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question