Solved

Get a file size in Delphi ...

Posted on 2012-03-27
7
2,211 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

679 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