×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!
  • Students Click Here

*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.

Students Click Here

Jobs

Size of file
2

Size of file

Size of file

(OP)
How to show with delphi size of the files in Mb, Kb etc.?

RE: Size of file

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

RE: Size of file

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

RE: Size of file

2
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

RE: Size of file

Thank you for that function, that definetly deservs a star! :)

jamesp0tter,
mr.jamespotter@gmail.com

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

RE: Size of file

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

RE: Size of file

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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!

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