Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Visual Basic
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.86/5 (3 votes)
5 Apr 2013CPOL1 min read 44.9K   9   11
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.

VB
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.

VB
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 4 Pin
Christian Amado2-Aug-12 10:39
professionalChristian Amado2-Aug-12 10:39 
GeneralDefinitions of the prefixes Pin
John Brett2-Aug-12 5:47
John Brett2-Aug-12 5:47 
GeneralRe: Definitions of the prefixes Pin
rctaubert2-Aug-12 9:11
rctaubert2-Aug-12 9:11 
GeneralRe: Definitions of the prefixes Pin
John Brett2-Aug-12 21:59
John Brett2-Aug-12 21:59 
SuggestionMuch simpler C# Version Pin
onelopez2-Aug-12 4:13
onelopez2-Aug-12 4:13 
GeneralRe: Much simpler C# Version Pin
rctaubert2-Aug-12 9:21
rctaubert2-Aug-12 9:21 
SuggestionRe: Much simpler C# Version Pin
Kim Nordmo5-Apr-13 5:04
Kim Nordmo5-Apr-13 5:04 
GeneralRe: Much simpler C# Version Pin
giammin5-Apr-13 5:06
giammin5-Apr-13 5:06 
GeneralRe: Much simpler C# Version Pin
George Swan7-Apr-13 11:18
mveGeorge Swan7-Apr-13 11:18 
GeneralRe: Much simpler C# Version Pin
John Brett7-Apr-13 21:32
John Brett7-Apr-13 21:32 
GeneralRe: Much simpler C# Version Pin
George Swan7-Apr-13 22:13
mveGeorge Swan7-Apr-13 22:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.