September 26, 2022, 07:12:10 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Return nicely formatted string showing file sizes...

Started by Kale, November 03, 2006, 05:17:13 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Kale

The code below returns nicely formatted strings showing file sizes in there respective units instead of just bytes. I saw this on another site in another language so i ported it over 'cos it's a handy sub. ;)


sub ReadableBytes(int64 Bytes, opt int DecimalPlaces = 0), string
{
String s;
Double Unit = Floor(Log(Bytes) / Log(1024));
s = Str$(Bytes / (1024 ^ Unit), DecimalPlaces * (Unit And 1));
Select (Unit)
{
Case 0: s = s + " Bytes";
Case 1: s = s + " Kb"; Case 2: s = s + " Mb";
Case 3: s = s + " Gb"; Case 4: s = s + " Tb";
Case 5: s = s + " Pb"; Case 6: s = s + " Eb";
}
return s;
}

global sub main()
{
Unsigned Int hFile;
hFile = OpenFile("C:\\Windows\\System32\\shell32.dll", MODE_READ);
if(hFile)
{
Print("shell32.dll = " + ReadableBytes(GetFileLength(hFile), 2));
CloseFile(hFile);
}

Print("");
Print(ReadableBytes(1024));
Print(ReadableBytes(5636356, 2));
Print(ReadableBytes(1024 * 1024 * 100, 1));
Print(ReadableBytes(10));

While (GetKey() = "")
{
}
}

Zen

Your right Kale, this is very handy, I made one like this in a project once, took me ages to get it right though. Im sure there are plenty of times when you need something like this.

Lewis