Solved

Get a file size in Delphi ...

Posted on 2012-03-27
7
1,915 Views
Last Modified: 2012-06-21
Hi Experts,

I need some help, i downloaded some code from the internet that will allow me to get a file size.

The Size on disk is:                                 9.42 GB (10,123,444,224 bytes)
This is the size its bringing back:         1533509120

This is the code im using:

function TFunctions.FileSize(FileName: string): Int64;
var
SearchRec: TSearchRec;
begin
if FindFirst(FileName, faAnyFile, SearchRec) = 0 then                  // if found
   //SearchRec.Size property works, but only for files less than 2GB
   Result := Int64(SearchRec.FindData.nFileSizeHigh) shl Int64(32) +    // calculate the size
             Int64(SearchRec.FindData.nFileSizeLow)
else
   Result := 0;
FindClose(SearchRec);                                                   // close the find
end;

Thank you in advance.


Trevor
0
Comment
Question by:trevsoft
7 Comments
 
LVL 19

Expert Comment

by:Thommy
Comment Utility
There's already an API function GetFileSize (unit windows)...

Check this link which shows and compares different ways for getting file size...
find the size of a file?
0
 
LVL 1

Author Comment

by:trevsoft
Comment Utility
Hi Thommy,

Thanks for that, tried that code too, for some reason i get the same result.

is there a way to use ShellAPI to do this.

Im assuming thats what windows is using when you right click on a file and go properties.



Trevor
0
 
LVL 19

Expert Comment

by:Thommy
Comment Utility
Following code determines drive cluster size and calculates size on disk...
Code is extracted from File size but size on Disk is required

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


function FileSizeEx(const Filename: string; var AFileSize, ASizeOnDisk, AClusterSize: int64): boolean;

function GetClusterSize(Drive: Char): Longint;
var
  SectorsPerCluster,  
  BytesPerSector,
  dummy: Cardinal;
begin
  SectorsPerCluster := 0;
  BytesPerSector := 0;
  GetDiskFreeSpace( PChar(Drive + ':\'), SectorsPerCluster,
  BytesPerSector, dummy, dummy );

  result := SectorsPerCluster * BytesPerSector;
end;

var
  SR : TSearchRec;
  n : integer;
begin
  if FindFirst(Filename, faAnyFile, SR) = 0 then
    try
      AFileSize := SR.FindData.nFileSizeHigh;
      AFileSize := (AFileSize shl (4 *sizeof(AFileSize))) or SR.FindData.nFileSizeLow;

      AClusterSize := GetClusterSize( UpCase(Filename[1]) );

      n := AFileSize mod AClusterSize;
      if n > 0 then
        ASizeOnDisk := AFileSize +(AClusterSize -n)
      else
        ASizeOnDisk := AFileSize;

      result := true;
  finally
    FindClose(SR);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  AFileSize,  
  ASizeOnDisk,
  AClusterSize : int64;
  s: string;
begin
  s:='test.exe';
  if FileSizeEx(s, AFileSize, ASizeOnDisk, AClusterSize) then
  ShowMessage( s +#10+
    Format('File size: %.n bytes', [AFileSize *1.0]) +#10+
    Format('Size on disk: %.n bytes', [ASizeOnDisk *1.0]) +#10+
    Format('Cluster size: %.n bytes', [AClusterSize *1.0]) )
  else
    ShowMessage('FileSizeEx() FAILED');

end;

end.

Open in new window

0
Do email signature updates give you a headache?

Do you feel like all of your time is spent managing email signatures? Too busy to visit every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

 
LVL 9

Expert Comment

by:rinfo
Comment Utility
Try this
function FileSize(const aFilename: String): Int64;
  var
    info: TWin32FileAttributeData;
  begin
    result := -1;

    if NOT GetFileAttributesEx(PWideChar(aFileName), GetFileExInfoStandard, @info) then
      EXIT;

    result := info.nFileSizeLow or (info.nFileSizeHigh shl 32);
  end;
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
also found this article for listing files
it did contain the same bug for file sizes
(and it got fixed)

http://www.delphiforfun.org/programs/Utilities/list_large_files.htm

code with the fixed filesize:
var
  F:TSearchrec;
  size:int64;
  fszhi,fszlo,m:int64;

        fszhi:=f.FindData.nfilesizehigh;
        fszlo:=f.FindData.nfilesizelow;
        m:=high(longword);
        inc(m,1);
        size:=fszhi*m+fszlo;

Open in new window

0
 
LVL 9

Expert Comment

by:rinfo
Comment Utility
Pwidechar maynot work in newer delphi version  pchar has to be used for older version.
0
 
LVL 25

Accepted Solution

by:
Sinisa Vuk earned 500 total points
Comment Utility
This is from most known - Rx lib - Fileutil.pas

function GetFileSize(const FileName: string): Int64;
var
  Handle: THandle;
  FindData: TWin32FindData;
begin
  Handle := FindFirstFile(PChar(FileName), FindData);
  if Handle <> INVALID_HANDLE_VALUE then begin
    Windows.FindClose(Handle);
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
      Int64Rec(Result).Lo := FindData.nFileSizeLow;
      Int64Rec(Result).Hi := FindData.nFileSizeHigh;
      Exit;
    end;
  end;
  Result := -1;
end;

Open in new window

0

Featured Post

Why do Marketing keep bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

Join & Write a Comment

This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now