Scalabium Software

SMExport advertising
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);
var
  arrSize: array[0..255] of Char;
begin
  {same formating like in statusbar of Explorer}
  StrFormatByteSize(70846, arrSize, Length(arrSize)-1);
  ShowMessage(Trim(arrSize));

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

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
 
Paradox to MS Access converter
DBLoad
Clarion Viewer
SMExport suite
ABA Document Convert
Paradox Password Recovery
Excel Reader (dll)
Paradox Viewer
ABA Picture Convert
DBISAM Viewer
 
 


Contact to webmaster

 

Borland Software Code Gear Scalabium Delphi tips

Copyright© 1998-2024, Scalabium Software. All rights reserved.
webmaster@scalabium.com

SMExport advertising