Discussion:
File Size
(too old to reply)
rob kemp
2008-07-15 00:26:38 UTC
Permalink
Does anyone know how to get the size of a file at runtime?

Something like this!!! GetFileSize("c:\\myfile.pdf");

Thanks in advance
Rob
Remy Lebeau (TeamB)
2008-07-15 00:53:19 UTC
Permalink
Post by rob kemp
Does anyone know how to get the size of a file at runtime?
Look at the VCL's FindFirst() function. The TSearchRec structure has Size
and FindData.nFileSizeLow/High members available.
Post by rob kemp
Something like this!!! GetFileSize("c:\\myfile.pdf");
unsigned __int64 __fastcall GetFileSize(const AnsiString &FileName)
{
TSearchRec sr;
if( FindFirst(FileName, faAnyFile, sr) == 0 )
{
FindClose(sr);
ULARGE_INTEGER ul;
ul.LowPart = sr.FindData.nFileSizeLow;
ul.HighPart = sr.FindData.nFileSizeHigh;
return ul.QuadPart;
}
else
return 0;
}


Gambit
Mark Jacobs
2008-07-16 12:39:47 UTC
Permalink
Post by rob kemp
Does anyone know how to get the size of a file at runtime?
Something like this!!! GetFileSize("c:\\myfile.pdf");
__int64 mjflsize(AnsiString flnm)
{
if (flnm.IsEmpty()) return 0;
struct stati64 statbuf;
if (_stati64(flnm.c_str(),&statbuf)==0) return statbuf.st_size;
return 0;
}

HTH,
--
Mark Jacobs
http://www.dkcomputing.co.uk
Michael Corby
2008-07-16 23:17:27 UTC
Permalink
int fileSize;
String work;

work = "C:\\Myfile.pdf";
TStream* streamWork = new TFileStream(work, fmOpenRead);
fileSize = streamWork->Size;
delete streamWork;

Michael Corby
Post by Mark Jacobs
Post by rob kemp
Does anyone know how to get the size of a file at runtime?
Something like this!!! GetFileSize("c:\\myfile.pdf");
__int64 mjflsize(AnsiString flnm)
{
if (flnm.IsEmpty()) return 0;
struct stati64 statbuf;
if (_stati64(flnm.c_str(),&statbuf)==0) return statbuf.st_size;
return 0;
}
HTH,
--
Mark Jacobs
http://www.dkcomputing.co.uk
Mark Jacobs
2008-07-17 12:26:34 UTC
Permalink
Post by Michael Corby
int fileSize;
String work;
work = "C:\\Myfile.pdf";
TStream* streamWork = new TFileStream(work, fmOpenRead);
fileSize = streamWork->Size;
delete streamWork;
Fails when file size exceeds 4GB. Mine does not. Allocates and deallocates memory. Mine
uses a static area in the C RTL. Opens the file to derive size. Mine reads the file
allocation table and does not open the file.

__int64 mjflsize(AnsiString flnm)
{
if (flnm.IsEmpty()) return 0;
struct stati64 statbuf;
if (_stati64(flnm.c_str(),&statbuf)==0) return statbuf.st_size;
return 0;
}
--
Mark Jacobs
http://www.dkcomputing.co.uk
Ed Mulroy [TeamB]
2008-07-17 22:16:04 UTC
Permalink
Nice, clean code. Now if you'd just format it the way I do (after all how I
do it is the PERFECT way to format code <g>)

Note that a third method, using FindFirstFile, FindClose is actually what
you are using, just that it's wrapped up in what __stati64 does.

. Ed
...Mine uses a static area in the C RTL. Opens the file to derive size.
Mine reads the file allocation table and does not open the file.
__int64 mjflsize(AnsiString flnm)
{
if (flnm.IsEmpty()) return 0;
struct stati64 statbuf;
if (_stati64(flnm.c_str(),&statbuf)==0) return statbuf.st_size;
return 0;
}
Remy Lebeau (TeamB)
2008-07-17 18:13:08 UTC
Permalink
Post by Michael Corby
TStream* streamWork = new TFileStream(work, fmOpenRead);
If the file does not exist, an exception will be thrown.
Post by Michael Corby
fileSize = streamWork->Size;
In BCB 6 and later, the Size property is an __int64 now.


Gambit
Loading...