Thread View: borland.public.cppbuilder.nativeapi
7 messages
7 total messages
Started by Robert G. Hoover
Mon, 09 Jul 2007 13:25
Get File Size in Bytes - Silly Question
Author: Robert G. Hoover
Date: Mon, 09 Jul 2007 13:25
Date: Mon, 09 Jul 2007 13:25
12 lines
1328 bytes
1328 bytes
Hi All, I'm sure there's a simple answer to this silly question... my apologies... How can I get the size of a file in bytes given it's absolute filename? I'd rather not open the file and perform a FileSeek to the end to determine the size... I need something quick, no overhead... does such a function exist? Something like GetFileSizeInBytes(Ansistring FileName)? Thanks, Rob
Re: Get File Size in Bytes - Silly Question
Author: Pete Fraser
Date: Mon, 09 Jul 2007 14:23
Date: Mon, 09 Jul 2007 14:23
18 lines
1549 bytes
1549 bytes
Some ideas here: http://www.codeproject.com/file/filesize.asp HTH Pete "Robert G. Hoover" <not.a.real.address@anywhere.sorry> wrote in message news:46923739@newsgroups.borland.com... > Hi All, > > I'm sure there's a simple answer to this silly question... > my apologies... > > How can I get the size of a file in bytes given it's > absolute filename? I'd > rather not open the file and perform a FileSeek to the end > to determine the > size... I need something quick, no overhead... does such a > function exist? > Something like GetFileSizeInBytes(Ansistring FileName)?
Re: Get File Size in Bytes - Silly Question
Author: Jonathan Benedicto
Date: Mon, 09 Jul 2007 14:25
Date: Mon, 09 Jul 2007 14:25
32 lines
2031 bytes
2031 bytes
Robert G. Hoover wrote:
> I'm sure there's a simple answer to this silly question... my apologies...
>
> How can I get the size of a file in bytes given it's absolute filename?
> I'd
> rather not open the file and perform a FileSeek to the end to determine
> the
> size... I need something quick, no overhead... does such a function exist?
> Something like GetFileSizeInBytes(Ansistring FileName)?
If you are ok with Win98 and higher, you could use the GetFileAttributesEx
function. Eg:
WIN32_FILE_ATTRIBUTE_DATA FileAttr;
if( GetFileAttributesEx( FileName, GetFileExInfoStandard, &FileAttr ) )
{
LARGE_INTEGER li;
li.LowPart = FileAttr.nFileSizeLow;
li.HighPart = FileAttr.nFileSizeHigh;
unsigned __int64 FileSizeInBytes = li.QuadPart;
}
Otherwise you'll have to open the file with CreateFile, then use GetFileSize
on the handle.
HTH
Jon
Re: Get File Size in Bytes - Silly Question
Author: Ed Mulroy [TeamB]
Date: Mon, 09 Jul 2007 14:29
Date: Mon, 09 Jul 2007 14:29
46 lines
2337 bytes
2337 bytes
See if this does what you want. If it does not find the file or if the file
does not have a size (directories and labels do not report size) then the
returned value is zero.
-------------------------------------
#include <vcl.h>
__int64 __fastcall SizeOfFile(const char *file_name);
inline __int64 SizeOfFile(AnsiString & as)
{
return SizeOfFile(as.c_str());
}
const HANDLE BAD_FIND_VALUE =
reinterpret_cast<HANDLE>(INVALID_HANDLE_VALUE);
__int64 __fastcall SizeOfFile(const char *file_name)
{
WIN32_FIND_DATA wfd;
HANDLE find_hand = ::FindFirstFile(file_name, &wfd);
__int64 result = 0;
if (find_hand != BAD_FIND_VALUE)
{
result = (static_cast<__int64>(wfd.nFileSizeHigh) << 32) |
wfd.nFileSizeLow;
}
return result;
}
-------------------------------------
. Ed
> Robert G. Hoover wrote in message
> news:46923739@newsgroups.borland.com...
>
> I'm sure there's a simple answer to this silly question... my apologies...
>
> How can I get the size of a file in bytes given it's absolute filename?
> I'd
> rather not open the file and perform a FileSeek to the end to determine
> the
> size... I need something quick, no overhead... does such a function exist?
> Something like GetFileSizeInBytes(Ansistring FileName)?
Re: Get File Size in Bytes - Silly Question
Author: Ed Mulroy [TeamB]
Date: Mon, 09 Jul 2007 14:33
Date: Mon, 09 Jul 2007 14:33
53 lines
2469 bytes
2469 bytes
Corrected version:
See if this does what you want. If it does not find the file or if the file
does not have a size (directories and labels do not report size) then the
returned value is zero.
-------------------------------------
#include <vcl.h>
__int64 __fastcall SizeOfFile(const char *file_name);
inline __int64 SizeOfFile(AnsiString & as)
{
return SizeOfFile(as.c_str());
}
const HANDLE BAD_FIND_VALUE =
reinterpret_cast<HANDLE>(INVALID_HANDLE_VALUE);
__int64 __fastcall SizeOfFile(const char *file_name)
{
WIN32_FIND_DATA wfd;
HANDLE find_hand = ::FindFirstFile(file_name, &wfd);
__int64 result = 0;
if (find_hand != BAD_FIND_VALUE)
{
result = (static_cast<__int64>(wfd.nFileSizeHigh) << 32) |
wfd.nFileSizeLow;
::FindClose(find_hand); // CORRECTION - added
}
return result;
}
-------------------------------------
>
> . Ed
>
>> Robert G. Hoover wrote in message
>> news:46923739@newsgroups.borland.com...
>>
>> I'm sure there's a simple answer to this silly question... my
>> apologies...
>>
>> How can I get the size of a file in bytes given it's absolute filename?
>> I'd
>> rather not open the file and perform a FileSeek to the end to determine
>> the
>> size... I need something quick, no overhead... does such a function
>> exist?
>> Something like GetFileSizeInBytes(Ansistring FileName)?
>
>
Re: Get File Size in Bytes - Silly Question
Author: Remy Lebeau (TeamB)
Date: Mon, 09 Jul 2007 17:07
Date: Mon, 09 Jul 2007 17:07
16 lines
1395 bytes
1395 bytes
"Robert G. Hoover" <not.a.real.address@anywhere.sorry> wrote in message news:46923739@newsgroups.borland.com... > How can I get the size of a file in bytes given it's absolute filename? You have two options: 1) open the file with CreateFile() and then query the size with GetFileSize(). 2) if you do not want to open the file, then use FindFirstFile() instead. The file size is part of the returned WIN32_FIND_DATA structure. Gambit
Re: Get File Size in Bytes - Silly Question
Author: Robert G. Hoover
Date: Mon, 09 Jul 2007 18:34
Date: Mon, 09 Jul 2007 18:34
6 lines
1316 bytes
1316 bytes
Outstanding!!! Guess you guys have run into this before. Thank you SO much for the suggestions. That's one more little crescent wrench for the ol' back pocket! Many thanks, Rob
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads