Scalabium Software

SMReport Autogenerated
Knowledge for your independence'.
Home Delphi and C++Builder tips

#165: How can I convert the file size to string (a-la Explorer)?

As you know, the Explorer from MS Windows allow to display the file sizes in "human" mode. For example, to display the "69.1 Kb" instead 70846, "1.61 Mb" instead 1697109 etc

Same formating used by Microsoft in Internet Explorer (during file downloading).

Note that in Explorer you may see the different formattings in statusbar and Size column (for detailed mode). For example, 70846 will be displayed as "69.1 Kb" in statusbar and as "70 Kb" in Size column.

You may write the own function and to divide the original size to 1024 a few times (depends from size), of course, but I suggest to use original functions - same that Microsoft use.

See small sample:

function StrFormatByteSize(dw: DWORD; szBuf: PChar; uiBufSize: UINT):
PChar; stdcall; external 'shlwapi.dll' name 'StrFormatByteSizeA';
function StrFormatKBSize(qdw: LONGLONG; szBuf: PChar; uiBufSize: UINT):
PChar; stdcall; external 'shlwapi.dll' name 'StrFormatKBSizeA';

procedure TForm1.Button15Click(Sender: TObject);
  arrSize: array[0..255] of Char;
  {same formating like in statusbar of Explorer}
  StrFormatByteSize(70846, arrSize, Length(arrSize)-1);

  {same formating like in Size column of Explorer in detailed mode}
  StrFormatKBSize(70846, arrSize, Length(arrSize)-1);

PS: note that for correct work, the shlwapi.dll must be installed in MS Windows. You can only get it by installing Internet Explorer 5 or later

Published: May 18, 2004

See also
Mail parser (ActiveX)
ABA Document Convert
Protected Storage Viewer
Excel Web-stream
Paradox to Text converter
Viewer for MS Outlook Messages
Fast Document Viewer

Contact to webmaster


Borland Software Code Gear Scalabium Delphi tips

Copyright© 1998-2017, Scalabium Software. All rights reserved.

SMExport advertising