README.md

December 5, 2022 ยท View on GitHub

FormatFileSize

Description

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.

More Info

dblFileSize: Double; File size in bytes

Optionally, pass a standard format string (e.g.: "###.##") in strFormatMask if you need to override the default formatting

Example:

"FormatFileSize(100)" will return "100 bytes"

"FormatFileSize(5500)" will return "5.4 KB"

"FormatFileSize(15000000)" will return "14.31 MB"

String

Submitted On
ByJeff Cockayne
LevelBeginner
User Rating4.3 (13 globes from 3 users)
CompatibilityVB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
CategoryString Manipulation
WorldVisual Basic
Archive File

Source Code

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