Backup com ProgressBar

Delphi

22/05/2006

Senhores,

Como eu poderia manipular um progress bar me informando a porcentagem atual da cópia que estou realizando em disquete?

Utilizo o seguinte procedimento, no qual faz cópia de arquivos de uma determinada pasta, sendo os arquivos SALVOS para um disquete.


Meu problema é que [b:5561af72a7]não sei como está o status da cópia e não informa se ela já finalizou,[/b:5561af72a7] por isso pensei em um progress bar.

Eis o procedimento

procedure TForm_Menu.BtnSalvarClick(Sender: TObject);
var
EMode: Word;
SR: TSearchRec;
I: integer;
Origem, Destino: string;
Begin

EMode := SetErrorMode(SEM_FAILCRITICALERRORS) ;
try
if DiskSize(Ord(´A´)-$40) <> -1 then
Begin
I := FindFirst(´C:\sistema\*.*´, faAnyFile, SR);
while I = 0 do begin
if (SR.Attr and faDirectory) <> faDirectory then begin
Origem := ´C:\sistema\´ + SR.Name;
Destino := ´a:\´ + SR.Name;

if FileExists(Destino) then
DeleteFile(Destino);

if not CopyFile(PChar(Origem), PChar(Destino), true) then
ShowMessage(´Erro ao copiar ´ + Origem + ´ para ´ + Destino);
end;
I := FindNext(SR);
end;
end
else
ShowMessage(´Disco não encontrado. Favor inserir um disquete!´) ;
finally
SetErrorMode(EMode) ;
end;

end;


Ce

Ce

Curtidas 0

Respostas

Okama

Okama

22/05/2006

Recebi esse código do Nildo, qualquer problema ou direitos autorais é com ele, hehe :lol:

FUNCTION TFormPrin.DoCopyFile(const SrcFile, DstFile: string; var progresso: TGauge): Boolean;
const
  bufSize = 50000;
var
  sStream,
  dStream : TFileStream;
  pBuf    : Pointer;
  cnt     : Integer;
  cnt2    : Integer;
  totCnt, strmSize: Int64;
begin
  Result := True;
  fPararCopia := False;
  totCnt := 0;

  try
    sStream := TFileStream.Create(SrcFile, fmOpenRead or fmShareDenyWrite);
  except on E: Exception do begin
        Result := False;
        Exit;
     end;
  end;

{  progresso.Visible := sStream.Size > ((1.5 * 1024) * 1024); // Maior que 1.5Mb}
  Progresso.MinValue := 0;
  Progresso.MaxValue := 100;
  Progresso.Progress := 0;

  strmSize := sStream.size;

   try  
     try 
        dStream := TFileStream.Create(DstFile, fmCreate or fmShareExclusive);
     except on E: Exception do begin
           Result := False;
           Exit;
        end;
     end; 

     try 
       GetMem(pBuf, bufSize);
       try 
         cnt := sStream.Read(pBuf^, bufSize);
         cnt := dStream.Write(pBuf^, cnt);

         totCnt := totCnt + cnt;
         while (cnt > 0) do begin
           If fPararCopia then
              Break;
           cnt := sStream.Read(pBuf^, bufSize);
           cnt2:= dStream.Write(pBuf^, cnt);

           if cnt2 <> cnt then
              Result := False;

           cnt := cnt2;

           totcnt := totcnt + cnt;
           progresso.Progress := Round((totCnt / strmSize) * 100);
           Application.ProcessMessages;
         end;

       finally
         FreeMem(pBuf, bufSize);
       end;
     finally
       dStream.Free;
       if fPararCopia then       {Se cancelar entao deleta o arquivo}
         DeleteFile(PChar(DstFile));
     end; 
   finally
     sStream.Free;
   end; 
end;



GOSTEI 0
POSTAR