Get size of file

before anyone gives me examples read on first.......


Does anyone have examples on how to use the Windows API to get the size of a file in bytes - must be able to handle HUGE files (ie: any size of file on a Windows system 4Gb upwards).

I want to use Windows API as i am assuming it is faster than going through the delphi way.
mi6agentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

geobulCommented:
Hi,

Try this:

function GetFileSize(const strFile: string): Int64;
var
  SearchRec:TSearchRec;
begin
  result := 0;
  if (FindFirst(strFile, faAnyFile, SearchRec)=0) then begin
    // File found.
    result := (Int64(SearchRec.FindData.nFileSizeHigh) shl 32) + SearchRec.FindData.nFileSizeLow;
  end;
  FindClose(SearchRec);
end;

usage:
ShowMessage(IntToStr(GetFileSize(Edit1.Text)));

Regards, Geo
0
DragonSlayerCommented:
Let's see if this works:

uses
  Windows;

function GetHugeSize(const S: string): Int64;
var
  fHandle: THandle;
  size: TLargeInteger;
begin
  Result := 0;
  fHandle := CreateFile(PChar(S), GENERIC_READ, 0, nil, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  try
    // here's the trick, making used of a record
    size.LowPart := GetFileSize(fHandle, @size.HighPart);
    Result := size.QuadPart;
  finally
    CloseHandle(fHandle);
  end;
end;



DragonSlayer.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DragonSlayerCommented:
LoL, sorry, didn't see Geo's comment... didn't press Refresh :-p
0
geobulCommented:
Another version (API only):

function GetFileSize(const strFile: string): Int64;
var
  h: THandle;
  FindData:TWin32FindData;
begin
  result := 0;
  h := windows.FindFirstFile(PChar(strFile), FindData);
  if (h > 0) then begin
    // File found.
    result := (Int64(FindData.nFileSizeHigh) shl 32) + FindData.nFileSizeLow;
  end;
  windows.FindClose(h);
end;

Regards, Geo
0
mi6agentAuthor Commented:
Thanks to both of you - i have raised the points and split them to give origianl 125 points each.

DragonSlayer - i used your code BUT changed OPEN_ALWAYS to OPEN_EXISTING - OPEN_ALWAYS will create the file if it is not found.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.