Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-07

Getting size of file without opening it?

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

I am using FindAllFiles in order to list the files in a directory and it works fine.

But I also need the size and timestamp of the file so I searched the net for it.

What I found was:

var T: TDateTime;
T := FileDateTodateTime(FileAge(filename));

But FileSize() needs a file argument to an open file!

This is not what I want, I may not even be allowed to open the file.

So is there a *cross-platform* way to retrieve the size of the files too?

Bo Berglund Developer in Sweden

Reply by email

Michael Van Canneyt
to Bo Berglund
Bo Berglund wrote 17 lines
I am using FindAllFiles in order to list the files in a directory and it works fine. But I also need the size and timestamp of the file so I searched the net for it. What I found was: var T: TDateTime; T := FileDateTodateTime(FileAge(filename)); But FileSize() needs a file argument to an open file! This is not what I want, I may not even be allowed to open the file. So is there a *cross-platform* way to retrieve the size of the files too?
uses sysutils;
Function GetFileSize(aFileName : String) : Int64;
Var
   Info : TSearchRec;
begin
   If FindFirst(aFileName,0,Info)=0 then
     Result:=Info.SIze
   else
     Result:=-1;
   FindClose(Info);
end;

Michael.

Reply by email

Bo Berglund
to Michael Van Canneyt
Michael Van Canneyt wrote 37 lines
On Fri, 24 Jul 2020, Bo Berglund via fpc-pascal wrote: > I am using FindAllFiles in order to list the files in a directory and > it works fine. > > But I also need the size and timestamp of the file so I searched the > net for it. > > What I found was: > > var T: TDateTime; > T := FileDateTodateTime(FileAge(filename)); > > But FileSize() needs a file argument to an open file! > > This is not what I want, I may not even be allowed to open the file. > > So is there a *cross-platform* way to retrieve the size of the files > too? uses sysutils; Function GetFileSize(aFileName : String) : Int64; Var Info : TSearchRec; begin If FindFirst(aFileName,0,Info)=0 then Result:=Info.SIze else Result:=-1; FindClose(Info); end;

Added to my "utils" unit. :-)
Thanks!

Bo Berglund Developer in Sweden

Reply by email