Jump to content
Fórum Script Brasil
  • 0

Função GetFileSize para arquivos maiores que 2GB no Windows


Question

Capturar.PNG.63f28895da81433772fc8dc4b38b7d58.PNG

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);
  }

 

Edited by Issei Hyoudou
Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      148692
    • Total Posts
      644524
×
×
  • Create New...