Click here to Skip to main content
13,248,766 members (57,794 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 2 Aug 2012

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

, 5 Apr 2013
Rate this:
Please Sign up or sign in to vote.
Convert filesize (bytes) according to highest possible size scale (KB, MB, GB, TB, PB).


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.

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
    For Each fileName As System.IO.FileInfo In filesInfo
        fileSize = fileName.Length
        myList = myList + roundObjectSize(fileSize.ToString) + vbNewLine
End Try


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

GeneralRe: Much simpler C# Version Pin
George Swan7-Apr-13 23:13
memberGeorge Swan7-Apr-13 23: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 5 Apr 2013
Article Copyright 2012 by bEGI23
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid