1. Computing
in Delphi TIPS :: When working with files from Delphi you might want to display the size of a file to the user in a Explorer-like format where the file size is not displayed in bytes - but the display depends on the size of the actual file.

Read the full article to learn how to Format a File Size in Bytes for Display

Related:

Comments
October 15, 2008 at 1:45 am
(1) Sven Söhnel says:

Hi @all!

First off all: sorry for my bad english.

I use an Unit “xProcs.pas” from Fabula.com since some years.

In this unit is an function i’ve extended to do such a format.
The “extended” version has the option to give the unit (KB/MB/GB/)
or use the next smaller one (hope you could understand):

[code]
{ xTool - Component Collection }
{ Copyright (c) 1995 Stefan Böther }

...

const
{ common computer sizes }
KBYTE = Sizeof(Byte) shl 10;
MBYTE = KBYTE shl 10;
GBYTE = MBYTE shl 10;

...

//The original one:
//function strByteSize(Value: Longint): String;
//
// function FltToStr(F: Extended): String;
// begin
// Result:=FloatToStrF(Round(F),ffNumber,6,0);
// end;
//
//begin
// if Value > GBYTE then
// Result:=FltTostr(Value / GBYTE)+' GB'
// else if Value > MBYTE then
// Result:=FltToStr(Value / MBYTE)+' MB'
// else if Value > KBYTE then
// Result:=FltTostr(Value / KBYTE)+' KB'
// else
// Result:=FltTostr(Value) +' Byte'; { 04.08.96 sb }
//end;

//strByteSize by Söhnel-Software'99

//adapted nr.1
function strByteSize2(bytes: Int64): string;
begin
//From HyperVirtualExplorer_Simple by Bill Miller
result := EmptyStr;
if (0 = 1024) and (bytes = 1048576) and (bytes = 1073741824) then
result := Format('%f GB', [bytes / 1073741824]);
end;

//the most used...
function strByteSize(Value: Real; Mode: string): string;

function FltToStr(F: Extended): string;
begin
Result := FloatToStrF(Round(F), ffNumber, 18, 0);
end;

begin
if Mode = '0' then
Result := FltTostr(Value);
if Mode = '' then
begin
if Value > GBYTE then
Result := FltTostr(Value / GBYTE) + ' GB'
else
if Value > MBYTE then
Result := FltToStr(Value / MBYTE) + ' MB'
else
if Value > KBYTE then
Result := FltTostr(Value / KBYTE) + ' KB'
else
Result := FltTostr(Value) + ' Byte'; { 04.08.96 sb }
exit;
end;
if Mode = '-1' then
begin
if Value > GBYTE then
Result := FltToStr(Value / MBYTE) + ' MB'
else
if Value > MBYTE then
Result := FltTostr(Value / KBYTE) + ' KB'
else
Result := FltTostr(Value) + ' Byte'; { 04.08.96 sb }
exit;
end;
if Mode = 'GB' then
begin
Result := FltTostr(Value / GBYTE) + ' GB';
end;
if Mode = 'MB' then
begin
Result := FltTostr(Value / MBYTE) + ' MB';
end;
if Mode = 'KB' then
begin
Result := FltTostr(Value / KBYTE) + ' KB';
end;
if Mode = 'B' then
begin
Result := FltTostr(Value) + ' Byte';
end;
end;
//strByteSize by Söhnel-Software'99

...

[/code]

December 21, 2010 at 7:05 pm
(2) Eitan says:

Hi Zarko and all,.

i have a small question:
wouldn’t it be more correct to do the last line like :
result := FormatFloat(‘#,### bytes’, bytes);

instead of the original:
result := FormatFloat(‘#.## bytes’, bytes);
?

i mean, is there a point do the dot between the “sharps”(#), or am i missing something?

sorry for my broken english, and thanks for yet another greate tip Zarko.

i’m a big fan :)
———

Sven, why did you do this?:
“if (0 = 1024)…”
i mean, it will always be False, so what’s the point?
———
p.s.
i’m not a programmer. Delphi is just my hobby,and i love it :)

January 4, 2012 at 8:55 am
(3) jaro schut says:

I hope this will be false for everytime,
bytes = 1024 is better

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.