Solved

Get a file size in Delphi ...

Posted on 2012-03-27
7
2,278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 37775122
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
ID: 37775244
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
ID: 37775394
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:rinfo
ID: 37776117
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 37

Expert Comment

by:Geert Gruwez
ID: 37776121
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
ID: 37776123
Pwidechar maynot work in newer delphi version  pchar has to be used for older version.
0
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 37841913
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

740 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