?
Solved

Get a file size in Delphi ...

Posted on 2012-03-27
7
Medium Priority
?
2,511 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
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
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 38

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 2000 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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
Configuring Remote Assistance for use with SCCM
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

752 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