File size calculator

On January 2, 2009, in Programming, by Mike Hudson

Use this simple VB.NET code to convert bytes to MB’s, GB’s etc. Handy for returning sizes of files/folders

Function ConvertSize(Size)
Do While InStr(Size,",") 'Remove commas from size
CommaLocate = InStr(Size,",")
     Size = Mid(Size,1,CommaLocate - 1) & _
       Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate)
       Loop</p> <p>Suffix = " Bytes"

   If Size >= 1024 Then suffix = " KB"
   If Size >= 1048576 Then suffix = " MB"
   If Size >= 1073741824 Then suffix = " GB"
   If Size >= 1099511627776 Then suffix = " TB" 

        Select Case Suffix
            Case " KB" Size = Round(Size / 1024, 1)
            Case " MB" Size = Round(Size / 1048576, 1)
            Case " GB" Size = Round(Size / 1073741824, 1)
            Case " TB" Size = Round(Size / 1099511627776, 1)
        End Select

        ConvertSize = Size & Suffix

End Function
Tagged with:  

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!