Click here to Skip to main content
Click here to Skip to main content

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

By , 5 Apr 2013
 

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

About the Author

bEGI23
Macedonia, The Republic Of Macedonia, The Republic Of
Member
Programming is my new hobby which i discovered few months ago.
I am ethnic Albanian from Macedonia, i speak the German language which i do master as my first language. Now i'm in process to learn mastering the English language. God willing!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberChristian Amado2 Aug '12 - 10:39 
GeneralDefinitions of the prefixesmemberJohn Brett2 Aug '12 - 5:47 
GeneralRe: Definitions of the prefixesmemberrctaubert2 Aug '12 - 9:11 
GeneralRe: Definitions of the prefixesmemberJohn Brett2 Aug '12 - 21:59 
SuggestionMuch simpler C# Versionmemberjlopez7882 Aug '12 - 4:13 
GeneralRe: Much simpler C# Versionmemberrctaubert2 Aug '12 - 9:21 
SuggestionRe: Much simpler C# VersionmemberKim Nordmo5 Apr '13 - 5:04 
GeneralRe: Much simpler C# Version [modified]membergiammin5 Apr '13 - 5:06 
GeneralRe: Much simpler C# VersionmemberGeorge Swan7 Apr '13 - 11:18 
GeneralRe: Much simpler C# VersionmemberJohn Brett7 Apr '13 - 21:32 
GeneralRe: Much simpler C# VersionmemberGeorge Swan7 Apr '13 - 22:13 

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

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