IonicWind Software

Aurora Compiler => Tips and Tricks => Topic started by: Kale on November 03, 2006, 05:17:13 PM

Title: Return nicely formatted string showing file sizes...
Post by: Kale on November 03, 2006, 05:17:13 PM
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() = "")
{
}
}
Title: Re: Return nicely formatted string showing file sizes...
Post by: Zen on November 06, 2006, 02:41:32 AM
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