Converting Bytes to a Human Readable String in VBScript

This is a little function I like to call “ReadableBytes”. It is so named because I’d often call properties of an object and their values would be returned in bytes. Well this would be fine and dandy but it got hard to tell how big a particular file, folder, or drive was at a glance when it was listed as a series of 5 to 10 (or more) numbers. At first I was doing sloppy math to decode these values on a case by case basis, but got sick of if-thens and weird equations.

It occurred to me that the scale used to describe a large number of bytes were exactly a power larger than the scale before it. I wrote a formula to convert these values quite easily:

ScaledValue = Bytes / 1024 ^ Nth

Depending on what number you passed as “N” you’d get an appropriate scale (bytes, kilobytes, etc.)

 

Leave a Comment