Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Size of file 2

Status
Not open for further replies.

Regany

Programmer
Aug 27, 2004
72
LV
How to show with delphi size of the files in Mb, Kb etc.?
 
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
 
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
 
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
 
Thank you for that function, that definetly deservs a star! :)

jamesp0tter,
mr.jamespotter@gmail.com

p.s.: sorry for my (sometimes) bad english :p
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top