How get filesize in cross-platform way on delphi xe2
Asked Answered
I

4

5

I have this rutine to know the filesize:

(Based on http://delphi.about.com/od/delphitips2008/qt/filesize.htm)

function FileSize(fileName : String) : Int64;
var
  sr : TSearchRec;
begin
  if FindFirst(fileName, faAnyFile, sr ) = 0 then
  {$IFDEF MSWINDOWS}
     result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + Int64(sr.FindData.nFileSizeLow)
  {$ELSE}
     result := sr.Size
  {$ENDIF}
  else
     result := -1;

  FindClose(sr) ;
end;

However, this give this warning:

[DCC Warning] Funciones.pas(61): W1002 Symbol 'FindData' is specific to a platform

I wonder if exist a clean cross-platform way to do this. I check TFile class and not found it...

Idun answered 3/2, 2012 at 0:56 Comment(1)
It depresses me that FindFirst appears to be the way to get file size information. It's counter intuitive and not even always accurate.Ectoderm
I
5

In Delphi XE2, the TSearchRec.Size member is already an Int64 (not sure which version that changed in) and is filled in with the full 64-bit value from the TSearchRec.FindData fields on Windows, so there is no need to calculate the size manually, eg:

{$IFDEF VER230}
  {$DEFINE USE_TSEARCHREC_SIZE}
{$ELSE}
  {$IFNDEF MSWINDOWS} 
    {$DEFINE USE_TSEARCHREC_SIZE}
  {$ENDIF} 
{$ENDIF}

function FileSize(fileName : String) : Int64; 
var 
  sr : TSearchRec; 
begin 
  if FindFirst(fileName, faAnyFile, sr ) = 0 then 
  begin
    {$IFDEF USE_TSEARCHREC_SIZE}
    Result := sr.Size;
    {$ELSE}
    Result := (Int64(sr.FindData.nFileSizeHigh) shl 32) + sr.FindData.nFileSizeLow;
    {$ENDIF} 
    FindClose(sr); 
  end
  else 
     Result := -1; 
end; 
Inerrable answered 3/2, 2012 at 2:19 Comment(2)
So is it in XE. I wouldn't be surprised if this goes all the way back to D4-D6 timeframe, and people kept the ifdef construct because it also worked on very old versions. But since nearly nobody still supports versions before D7, I think it is time to kill the beast.Sect
It switched to an Int64 in Delphi 2006.Lida
F
4

The warning you are getting because the FindData member of the TSearchRec structure is specific to Windows platform, but you don't need to worry about it because in your code you are not accessing that member when you are on the platform different from Windows.

// condition if you are on the Windows platform
{$IFDEF MSWINDOWS}
  // here you can access the FindData member because you are
  // on Windows
  Result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + 
    Int64(sr.FindData.nFileSizeLow);
{$ELSE}
  // here you can't use FindData member and you would even 
  // get the compiler error because the FindData member is 
  // Windows specific and you are now on different platform
{$ENDIF}
Fixture answered 3/2, 2012 at 1:1 Comment(3)
@TLama, this does not remove the warning.Carlyn
But @François, the question is if there is a cross platform solution (what OP already have), not how to suppress the warning ;) But I like your way (+1ed)Fixture
Yep, probably a better question could be used, I can think how do it. But the intention was be crossplataform and remove the warning. More exactly, a native delphi xe2 way to do it.Idun
C
4

Because you already check you are running on Windows, it is safe to remove locally the Warning to keep only "real" warnings reported by the compiler:

  if FindFirst(fileName, faAnyFile, sr ) = 0 then
  {$IFDEF MSWINDOWS}
    {$WARN SYMBOL_PLATFORM OFF}
     result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + Int64(sr.FindData.nFileSizeLow)
    {$WARN SYMBOL_PLATFORM ON}
  {$ELSE}
Carlyn answered 3/2, 2012 at 1:32 Comment(0)
D
-1
TDirectory.GetLastWriteTime(path);
Dorsey answered 31/3, 2016 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.