Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Convert filesize (bytes) according to highest possible size scale (KB, MB, GB, TB, PB)

0.00/5 (No votes)
5 Apr 2013 1  
Convert filesize (bytes) according to highest possible size scale (KB, MB, GB, TB, PB).

Introduction 

This code I uploaded may be helpful to some software developers who are in the beginning of their software programming career and who do not know much about file size converting. http://www.youtube.com/user/MCneun.

There are two ways to define data size, to convert it into the appropriate upper size scale. Most people think that 1 MB is 1000*1000 bytes, which isn't correct. 1024*1024 bytes match 1 MB and 1000*1000*1000 bytes doesn't match 1 GB, but they match 976MB. In reality, 1GB is 1024MB.

Using the code

The first function checks if the result is a decimal number, with comma, which isn't a full number. Accordingly we format the result to show only the last two digits after the comma. Even if results are full numbers, we format it and add a comma and two zeros after the full number, making the end result look more precise.

For 0 to 999 bytes the final result comes according to its real calculation result. Results which are greater than 1000 (till 1023) we round them to the next upper scale, let's say to 1,00 KB. And so on. We want to prevent results with four digits, so we round them up.

This method is the same as Windows using it to convert data size.

Private Function checkIfValueIsDecimal(ByVal value As String) As String
    Dim result As String
    If value.Contains(",") Then : result = CDbl(value).ToString("###.##")
    Else : result = CDbl(value).ToString("###.00") : End If
    Return result
End Function

Private Function roundObjectSize(ByVal ObjectSize As String) As String
    Dim oneByte As Integer = 1
    Dim kiloByte As Integer = 1024
    Dim megaByte As Integer = 1048576
    Dim gigaByte As Integer = 1073741824
    Dim terraByte As Long = 1099511627776
    Dim pettaByte As Long = 1125899906842624

    Select Case CLng(ObjectSize)
        Case 0 To kiloByte - 1
            If (CDbl(checkIfValueIsDecimal(CStr(CDec(ObjectSize) / oneByte))) >= 1000) = False Then
                ObjectSize = CStr(CInt(ObjectSize) / 1) + " Bytes"
            Else : ObjectSize = "1,00 KB" : End If

        Case kiloByte To megaByte - 1
            If (CDbl(checkIfValueIsDecimal(CStr(CDec(ObjectSize) / kiloByte))) >= 1000) = False Then
                ObjectSize = checkIfValueIsDecimal(CStr(CDec(ObjectSize) / kiloByte)) + " KB"
            Else : ObjectSize = "1,00 MB" : End If

        Case megaByte To gigaByte - 1
            If (CDbl(checkIfValueIsDecimal(CStr(CDec(ObjectSize) / megaByte))) >= 1000) = False Then
                ObjectSize = checkIfValueIsDecimal(CStr(CDec(ObjectSize) / megaByte)) + " MB"
            Else : ObjectSize = "1,00 GB" : End If

        Case gigaByte To terraByte - 1
            If (CDbl(checkIfValueIsDecimal(CStr(CDec(ObjectSize) / gigaByte))) >= 1000) = False Then
                ObjectSize = checkIfValueIsDecimal(CStr(CDec(ObjectSize) / gigaByte)) + " GB"
            Else : ObjectSize = "1,00 TB" : End If

        Case terraByte To pettaByte - 1
            If (CDbl(checkIfValueIsDecimal(CStr(CDec(ObjectSize) / terraByte))) >= 1000) = False Then
                ObjectSize = checkIfValueIsDecimal(CStr(CDec(ObjectSize) / terraByte)) + " TB"
            Else : ObjectSize = "1,00 PB" : End If
    End Select
    Return ObjectSize
End Function

To use it (example):

If you want to convert file sizes you must refer to file(s) folder (in this case). Copy and paste the below code into a Sub button or wherever you want to trigger this code.

Dim drvInfo As System.IO.DirectoryInfo = New System.IO.DirectoryInfo("C:\myFolder")
Dim sourceFolderSize() As System.IO.FileSystemInfo
Dim filesInfo() As System.IO.FileInfo = _
       drvInfo.GetFiles("*.*", System.IO.SearchOption.TopDirectoryOnly)
sourceFolderSize = drvInfo.GetFileSystemInfos()
Dim fileSize As Long = 0
Dim myList As String = Nothing
Try
    For Each fileName As System.IO.FileInfo In filesInfo
        fileSize = fileName.Length
        myList = myList + roundObjectSize(fileSize.ToString) + vbNewLine
    Next
Catch
End Try
MsgBox(myList)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here