Função GetFileSize para arquivos maiores que 2GB
função convertida do Delphi para C++ Build para quem está precisando, testado com arquivos de 700MB ate 10GB
// Função em Delphi
function GetFileSize( filename: string):string;
const
K = Int64(1024);
M = K * K;
G = K * M;
T = K * G;
var
size: Int64;
handle: integer;
begin
handle := FileOpen(filename, fmOpenRead);
if handle = -1 then
result := 'ERROR'
else try
size := FileSeek ( handle, Int64(0), 2 );
if size < K then result := Format ( '%d bytes', )
else if size < M then result := Format ( '%f KB', )
else if size < G then result := Format ( '%f MB', )
else if size < T then result := Format ( '%f GB', )
else result := Format ( '%f TB', );
finally
FileClose ( handle );
end;
end;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Função em C++
AnsiString GetFileSize(AnsiString filename)
{
float TB = 1099511627776;
float GB = 1073741824;
float MB = 1048576;
float KB = 1024;
__int64 FILE_OPEN, FILE_KB;
float FILE_BYTES;
char FILE_CHAR[32] = "";
char * FILE_STR;
FILE_OPEN = FileOpen(filename, fmOpenRead);
if(FILE_OPEN == -1)
{
return "ERROR";
}
else
try
{
FILE_KB = FileSeek(FILE_OPEN, __int64(0), 2);
FILE_BYTES = FILE_KB;
FILE_STR = FILE_CHAR;
if (FILE_BYTES >= TB)
sprintf(FILE_STR, "%.2f TB", (float)FILE_BYTES / TB);
else
if (FILE_BYTES >= GB && FILE_BYTES < TB)
sprintf(FILE_STR, "%.2f GB", (float)FILE_BYTES / GB);
else
if (FILE_BYTES >= MB && FILE_BYTES < GB)
sprintf(FILE_STR, "%.2f MB", (float)FILE_BYTES / MB);
else
if (FILE_BYTES >= KB && FILE_BYTES < MB)
sprintf(FILE_STR, "%.2f KB", (float)FILE_BYTES / KB);
else
if (FILE_KB < KB)
sprintf(FILE_STR, "%.2f Bytes", FILE_BYTES);
else
sprintf(FILE_STR, "%.2f Bytes", FILE_BYTES);
}
catch(...)
{
FileClose(FILE_OPEN);
}
return FILE_STR;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Exemplo de Uso
if (OpenDialog1->Execute())
{
Edit2->Text = GetFileSize(OpenDialog1->FileName);
}