Use this simple VB.NET code to convert bytes to MB’s, GB’s etc. Handy for returning sizes of files/folders
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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 |