Topic: GetFileSize Function

Hello Dmitry,

Is there anyway to use the GetFileSize function to display the size in MB of the files you attach to an e-mail? I want to display on a label, the
size in MB of files being attached so that when it reaches the maximum of attachments size (in this case 15MB ) the label displays the size in
RED.  Otherwise, if it's less than 15MB displays it in GREEN.

Here's a sample code I copied for the attachments: (Note: This code is not working, I'm not sure how to use the GetFileSize function  for
my purposes here .. )

procedure Form1_attached_OnClick (Sender: TObject; var Cancel: boolean);
var
    OpenDialog: TOpenDialog;
    d: int64;
    i: integer;
begin

    OpenDialog := TOpenDialog.Create(Form1);

    try
        if OpenDialog.Execute then
        begin
            if FileExists(OpenDialog.FileName) then Form1.mmfiles.Lines.Add(OpenDialog.FileName);
            d := GetFileSize(OpenDialog.FileName);
            i := d;
            Form1.MBSize.Caption := FormatFloat('0.00', i/(1024*1024)); //MB
        end;
    finally
        OpenDialog.Free;
    end;
end;


Thanks , appreciate your help!

2 (edited by ehwagner 2019-09-06 16:56:53)

Re: GetFileSize Function

gonpublic2k,

Change the following line:

Form1.MBSize.Caption := FormatFloat('0.00', i/(1024*1024)); //MB

To

  If i/(1024*1024) < 1 then Form1.MBSize.Caption := '<1MB'
               else Form1.MBSize.Caption := FormatFloat('0.00', i/(1024*1024)) + 'MB'; //MB
  If i/(1024*1024) > 15 then Form1.MBSize.Font.Color := clRed
               else Form1.MBSize.Font.Color := clGreen;

Re: GetFileSize Function

ehwagner wrote:

gonpublic2k,

Change the following line:

Form1.MBSize.Caption := FormatFloat('0.00', i/(1024*1024)); //MB

To

  If i/(1024*1024) < 1 then Form1.MBSize.Caption := '<1MB'
               else Form1.MBSize.Caption := FormatFloat('0.00', i/(1024*1024)) + 'MB'; //MB
  If i/(1024*1024) > 15 then Form1.MBSize.Font.Color := clRed
               else Form1.MBSize.Font.Color := clGreen;

Thank you ehwagner, this works but it doesn't show the actual size of the files being attached until after you exceed the limit of 15MB.  I want to
show in a label, real time size of the files being attached.  I don't know if i'm explaining this well.

Re: GetFileSize Function

I'm not sure I understand. I tested my changes in the procedure you provided and it displays the file size in MB whether it is below or above 15. Are you changing something when you retrofit this code into your actual project?


You mention real time size. I don't understand what you mean. If you are looking to display the actual size of a file then all you need to do is create another label and then populate it with the integer i (see below).

Form1.ActualSize.Caption := IntToStr(i);

Re: GetFileSize Function

ehwagner wrote:

I'm not sure I understand. I tested my changes in the procedure you provided and it displays the file size in MB whether it is below or above 15. Are you changing something when you retrofit this code into your actual project?


You mention real time size. I don't understand what you mean. If you are looking to display the actual size of a file then all you need to do is create another label and then populate it with the integer i (see below).

Form1.ActualSize.Caption := IntToStr(i);

Basically I need the label "MBSize" to update when you add another file.  As you attach files, the label updates with the size of the archives
already added (Total sum of files in MB), to show you that you're still under 15MB.  If you go over 15MB then it displays the size in MB in
red color. 

Your script works great , it's just the part to update the lable as you add more files that needs to be working.

6 (edited by ehwagner 2019-09-06 21:49:43)

Re: GetFileSize Function

Ok I understand now. See attached and see if that can work for you.

Post's attachments

Attachment icon SumFileSizes.zip 337.03 kb, 357 downloads since 2019-09-06 

Re: GetFileSize Function

ehwagner wrote:

Ok I understand now. See attached and see if that can work for you.

Excellent @ehwaganer!  you  are the man!  That works out pretty well. kudos!