Forum > Beginners

Copy file with progress bar

(1/2) > >>

Diverod:
If I set the property of my progesssbar to BarShowText to True in Object Inspector my procedure no longer works (It works fine otherwise). I don’t really need this aspect to work but would like to understand why not if possible.

Lazarus 2.2.0
FPC 3.2.2
Debian package 2.2.0+dfsg1-5ubuntu1 x86_64-linux-gtk2
Ubuntu 22.04 windowing system X11 

I’m trying to move away from windows and found Lazarus, what a jewel, thanks so much to the developers.

Below is a small test app. The Form has a button, label and progressbar.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls; type   { TForm1 }   TForm1 = class(TForm)    btnCopyFile: TButton;    lblCopyTime: TLabel;    pgbCurrentFile: TProgressBar;    procedure btnCopyFileClick(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 }// If you set the ProgressBar to Smooth it will slow the procedure extremely// If you set the ProgressBar BarShowText to True this will not work. procedure CopyFileWithPgB(Source, Destination: string; PgB: TProgressBar; LbL:                          TLabel);// Do FileExists before calling ________________________________________________var  FromFile, ToFile: file of byte;  Buffer: array[0..4096] of char;  NumRead: integer;  FileLength: longint;  StartTime, EndTime: TDateTime;     // Used to show the CopyTime to copy a filebegin  AssignFile(FromFile, Source);  reset(FromFile);  AssignFile(ToFile, Destination);  rewrite(ToFile);  FileLength := FileSize(FromFile);  with PgB do  begin    Max:= FileLength;    StartTime:= now;                 // Used to show the CopyTime to copy a file     while FileLength > 0 do    begin      BlockRead(FromFile, Buffer[0], SizeOf(Buffer), NumRead);      FileLength:= FileLength - NumRead;      BlockWrite(ToFile, Buffer[0], NumRead);      Position:= Position + NumRead;      EndTime:= now;                 // Used to show the CopyTime to copy a file      LbL.Caption:= FormatDateTime('hh:nn:ss', EndTime - StartTime);      Application.Processmessages;    end;    CloseFile(FromFile);    CloseFile(ToFile);  end;end; procedure TForm1.btnCopyFileClick(Sender: TObject);var Source, Dest: string;begin// Do prechecks to make sure FileExists_________________________________________Source:='/home/rodg/Desktop/Source/Test.mkv';Dest:= '/home/rodg/Desktop/Destination1/Test.mkv';       pgbCurrentFile.Position:= 0;       CopyFileWithPgB(Source, Dest, pgbCurrentFile, lblCopyTime); end; end. 

jamie:
Try calling "Update" within your loop which should be the Progressbar.

Also, you can try calling "Repaint";

Bart:
A few remarks:

* If something goes wrong reading, and numread = 0, you'll have an infinite loop. Common practice is to stop the loop when Numread <> SizeOf(Buffer)
* There's no error checking
* IIRC then the Position property has a max on either Windows or *nix, so it won't work as expected for very large files. Better calculate the percentage copied, set MaxValue of the progressbar to 100, round (or floor or ceil) the percentage and use that as the value for Position
Bart

jamie:
I also notice the buffer size is not on an even multiple, maybe it should be [0..4095] instead ?

This may not matter with linux io.

Josh:
Proj Attached a copyfile, it uses a modified laz/fpc copydir unit, it was done a couple of years back so cant remember all the mods, mainly to do with progress monitoring and copy file attribs and date etc, think i added in configurable buffer size iirc.

if you untick copy all files, you get presented with extra functions, these were added.

Navigation

[0] Message Index

[#] Next page

Go to full version