by Jeff Cockayne (2 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (3 Votes)
FormatFileSize: Formats a file's size in bytes into X GB or X MB or X KB or X bytes depending on size (a la Win9x Properties tab)
* UPDATED Sept. 12, 2000 * to allow for overriding the default Format Mask.
Inputs
dblFileSize: Double; File size in bytes
Optionally, pass a standard format string (e.g.: "###.##") in strFormatMask if you need to override the default formatting
Assumes
Example:
"FormatFileSize(100)" will return "100 bytes"
"FormatFileSize(5500)" will return "5.4 KB"
"FormatFileSize(15000000)" will return "14.31 MB"
Code Returns
String
Side Effects
None.
Public Function FormatFileSize(ByVal dblFileSize As Double, _
Optional ByVal strFormatMask As String) _
As String
' FormatFileSize: Formats dblFileSize in bytes into
' X GB or X MB or X KB or X bytes depending
' on size (a la Win9x Properties tab)
Select Case dblFileSize
Case 0 To 1023 ' Bytes
FormatFileSize = Format(dblFileSize) & " bytes"
Case 1024 To 1048575 ' KB
If strFormatMask = Empty Then strFormatMask = "###0"
FormatFileSize = Format(dblFileSize / 1024#, strFormatMask) & " KB"
Case 1024# ^ 2 To 1073741823 ' MB
If strFormatMask = Empty Then strFormatMask = "###0.0"
FormatFileSize = Format(dblFileSize / (1024# ^ 2), strFormatMask) & " MB"
Case Is > 1073741823# ' GB
If strFormatMask = Empty Then strFormatMask = "###0.0"
FormatFileSize = Format(dblFileSize / (1024# ^ 3), strFormatMask) & " GB"
End Select
End Function