Solved

Get a file size in Delphi ...

Posted on 2012-03-27
7
2,057 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
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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 26

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Installing trusted root certificate 12 49
problem to connect remoteley to my brother laptop (windows81.1 pro ) 10 40
Windows NLB cluster 3 30
Cygwin - GNU GPL License 1 24
Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

778 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