Skip to content

Instantly share code, notes, and snippets.

@moodyjim
Last active October 20, 2015 16:57
Show Gist options
  • Save moodyjim/1f9ca4630c36931950d1 to your computer and use it in GitHub Desktop.
Save moodyjim/1f9ca4630c36931950d1 to your computer and use it in GitHub Desktop.
VBScript: Convert bytes to KB/MB/GB/TB
Function ConvertSize(byteSize)
dim Size
Size = byteSize
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment