Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Regany (Programmer) (OP)
5 Nov 04 15:44
How to show with delphi size of the files in Mb, Kb etc.?
gcaramia (Programmer)
6 Nov 04 3:16

CODE

function GetFileSize(sFileToExamine: string): string;
var SearchRec: TSearchRec;
    sgPath: string;
    inRetval, I1: Integer;
begin
  sgPath :=ExpandFileName(sFileToExamine);
  try
    inRetval :=FindFirst(ExpandFileName(sFileToExamine), faAnyFile, SearchRec);
    if inRetval=0 then I1 :=SearchRec.Size else I1 :=-1;
  finally
    SysUtils.FindClose(SearchRec);
  end;
  Result :=IntToStr(I1);
end;

Giovanni Caramia
sijgs (Programmer)
6 Nov 04 6:45
Perhaps you meant something along the following lines (sorry, but it's too late to work out the float-to-two-digits-add-"Kb/Mb/Gb")

CODE

Var
  F  : File;
  FS : Cardinal;
begin
  assignFile(F, ParamStr(1));
  {$I-}
  Reset (F, 1);
  {$I+}
  If (IOResult <> 0) Then
    ShowMessage('Whoops! you really didn''t mean THAT file did you?')
  Else
    Begin
      FS := FileSize(F);
      If (FS = 0) Then
        ShowMessage('So just how did you manage to create a 0 length file?')
      Else
        Begin
          If ((FS div 1024) < 1024) Then // Bytes
            ShowMessage('')
          Else
            If (FS Div (1024 * 1024) < 1024) Then  // Kb
              ShowMessage('')
            Else
              If (FS Div (1024 * 1024 * 1024) < 1024) Then // Mb
                ShowMessage('')
             // :
             // etc
             // :
             // ad nauseum
         End;
     End;

Regards and HTH,
JGS

Helpful Member!(2)  towerbase (Programmer)
6 Nov 04 11:09
Both previous solutions give incorrect results if the file size is more than 2 GB.  The following function gives correct results for files up to 999 TB which should be adequate for the next few years.

CODE

function FileSizeStr ( filename: string ): string;
var
  size: Int64;
  handle: integer;
begin
  handle := FileOpen(filename, fmOpenRead);
  if handle = -1 then
    result := 'Unable to open file ' + filename
  else try
    size := Int64(FileSeek(handle,Int64(0),2));
    case Length(IntToStr(size)) of
      1..3: result := Format ( '%d bytes', [size] );
      4..6: result := Format ( '%f KB', [size / 1E3] );
      7..9: result := Format ( '%f MB', [size / 1E6] );
      10..12: result := Format ( '%f GB', [size / 1E9] );
      13..15: result := Format ( '%f TB', [size / 1E12] );
    end;
  finally
    FileClose ( handle );
  end;
end;
The original posting did not specify how the output should be formatted.  The above function produces output such as:
5.24 GB
512 bytes
12.35 KB
It is easy to change the format strings if a different format is required.

Andrew
Hampshire, UK

jamesp0tter (Programmer)
7 Nov 04 16:50
Thank you for that function, that definetly deservs a star! :)

jamesp0tter,
mr.jamespotter@gmail.com

p.s.: sorry for my (sometimes) bad english :p

sijgs (Programmer)
7 Nov 04 18:52
TowerBase:

You are absolutely correct that there would be an error above 2GB in my solution.

However, in keeping with "correctness", last time I looked, 1E3(=1*10**3), 1E6(=1*10**6), 1E9(=1*10**9) produces 1,000, 1,000,000, and 1,000,000,000 respectively.  

Checking Windows filesize reporting, 1Kb is 1024 bytes, 1 Mb is (1024 ** 2) or 1,048,576 bytes, and 1 Gb is (1024 ** 3) or 1,073,741,824 bytes.

No?

Regards,
JGS
towerbase (Programmer)
8 Nov 04 4:58
Yes, you are quite right.  1E3 = 1*10**3.

However, K is often interpreted as either 1000 or 1024.  Many hard drive manufacturers use K as 1000 as it makes their products appear larger.  Your employer will use K = 1000 when calculating your pay.  K is an abbreviation for Kilo which is the prefix that usually means 1000 but in the IT industry frequently means 1024.

I've modified the function to allow K to mean either 1000 or 1024.  In doing so, I might have found a small bug in Delphi (7) because it doesn't allow Int64 to be used as a selector expression in a case statement despite the Help saying "where selector Expression is any expression of an ordinal type".

CODE

function FileSizeStr ( filename: string ): string;
const
//   K = Int64(1000);     // Comment out this line OR
  K = Int64(1024);     // Comment out this line
  M = K * K;
  G = K * M;
  T = K * G;
var
  size: Int64;
  handle: integer;
begin
  handle := FileOpen(filename, fmOpenRead);
  if handle = -1 then
    result := 'Unable to open file ' + filename
  else try
    size := FileSeek ( handle, Int64(0), 2 );
    if size < K then result := Format ( '%d bytes', [size] )
    else if size < M then result := Format ( '%f KB', [size / K] )
    else if size < G then result := Format ( '%f MB', [size / M] )
    else if size < T then result := Format ( '%f GB', [size / G] )
    else result := Format ( '%f TB', [size / T] );
  finally
    FileClose ( handle );
  end;
end;

Andrew
Hampshire, UK

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close