13 Maret 2009

Copy File dengan Progress Dialog

Bagi yang sering mengcopy file pada Windows terutama pada Explorer, pasti pernah melihat progress dialog yang ditampilkan selama proses copy file berlangsung. Pada Delphi ada procedure untuk copy file yaitu Windows.CopyFile(), tapi procedure ini tidak menampilkan progress dialog. Berikut ini contoh untuk copy beberapa file dengan menampilkan progress dialog selama proses copy berlangsung.
uses ShellAPI;

procedure Copy_File;
var
  Struct: TSHFileOpStruct;
  Buf: array [0..4096] of Char;
  p: PChar;
  DestFile: string;
begin
  FillChar(Buf, SizeOf(Buf), #0);
  p := @Buf;
  p := StrECopy(p, 'C:\File1.txt') + 1;
  p := StrECopy(p, 'C:\File2.txt') + 1;
  StrECopy(p, 'C:\File3.txt') ;
  DestFile := 'D:\';
  Struct.Wnd := 0;
  Struct.wFunc := FO_COPY;
  Struct.pFrom := @Buf;
  Struct.pTo := PChar(DestFile);
  Struct.fFlags:= 0;
  if ((SHFileOperation(Struct) <> 0) or (Struct.fAnyOperationsAborted <> False)) then
    ShowMessage('Proses dibatalkan');
end;
Procedure diatas akan mengcopy 3 file (file1.txt, file2.txt, file3.txt) dari “C:\” ke “D:\”. Yang perlu diperhatikan disini adalah buffer yang berisi nama file yang akan dicopy harus diakhiri dengan 2 karakter #0. Tidak hanya untuk copy file, tetapi bisa juga hapus/memindahkan/rename file dengan mengganti flag dari FO_COPY ke FO_DELETE / FO_MOVE / FO_RENAME.

Tidak ada komentar:

Posting Komentar