Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return KB, MB and GB from Bytes using a public function

I'm writing a "function" that returns a file's size (in B, KB, MB, GB).

The VB.Net code always gets the size in bytes first, so when a file's size (in Bytes) is less than 100 it returns B, if its > 1000 then I divide it by 1000 and it returns KB. But when it should be MB I try dividing by 1000000 and the size it returns always comes out 2 MBs bigger than what it should be!?

Can someone please tell me what I'm doing wrong!!

Example

My files size is (15,570,550 Bytes) ..which is.. (14.8 MB)

So when i run it through this function it returns 16MB!

The Code

Public Function GetFileSize(ByVal TheFile As String, _
                            Optional ByVal ShowSizeType As Boolean = False) As String
    If TheFile.Length = 0 Then Return ""
    If Not System.IO.File.Exists(TheFile) Then Return ""
    '---
    Dim TheSize As Integer = My.Computer.FileSystem.GetFileInfo(TheFile).Length
    Dim SizeType As String = ""
    '---
    If TheSize < 1000 Then
        SizeType = "B"
    Else
        If TheSize < 1000000000 Then
            If TheSize < 1000000 Then
                SizeType = "KB"
                TheSize = TheSize / 1000
            Else
                SizeType = "MB"
                TheSize = TheSize / 1000000
            End If
        Else
            SizeType = "GB"

        End If
    End If
    '---
    If ShowSizeType = True Then
        Return TheSize & SizeType
    Else
        Return TheSize
    End If
End Function
like image 448
Mean Mr Mustard Avatar asked Dec 08 '14 21:12

Mean Mr Mustard


People also ask

How do you convert bytes to GB manually?

The simplest way to convert bytes to gigabytes is to divide the value of bytes by 1,000,000,000 and the result that you will get will be in the form of gigabytes.

What is the order of KB MB GB TB?

KB, MB, GB - A kilobyte (KB) is 1,024 bytes. A megabyte (MB) is 1,024 kilobytes. A gigabyte (GB) is 1,024 megabytes. A terabyte (TB) is 1,024 gigabytes.

What is KB MB GB TB and PB?

A kilobyte (KB) is 1,000 bytes, and one megabyte (MB) is 1,000 kilobytes. One gigabyte (GB) is equal to 1,000 megabytes, while a terabyte (TB) is 1,000 gigabytes.

How do I convert MB to file size?

So to get the MB size, you need to divide the file size from (1024*1024).


2 Answers

I would use a select case for it and not a if.
And always start with the biggest size." i stopped at TB but of Corse you can add more if you need ..."

I changed Dim TheSize As Integer to "Dim TheSize As ULong " otherwise big numbers don't work.

Also make a dim "Dim DoubleBytes As Double" you will use it in the select case.

First you compare the bytes you have with a case , lets say mb "Case 1048576 To 1073741823"
So if this is the case convert TheSize to a double "DoubleBytes = CDbl(TheSize / 1048576) 'MB "

Then in the return you use FormatNumber to set the number you want to show behind the . "the nuber 2 is to set it to 2 behind the . like 28.11 , change it to 0 and it will return 28" also because you know it mb you wil add & mb to the return.
"Return FormatNumber(DoubleBytes, 2) & " MB" "

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MsgBox(GetFileSize("E:\Software\TeamSpeak3-Client-win64-3.0.14.exe"))
End Sub

Dim DoubleBytes As Double

Public Function GetFileSize(ByVal TheFile As String) As String
    If TheFile.Length = 0 Then Return ""
    If Not System.IO.File.Exists(TheFile) Then Return ""
    '---
    Dim TheSize As ULong = My.Computer.FileSystem.GetFileInfo(TheFile).Length
    Dim SizeType As String = ""
    '---

    Try
        Select Case TheSize
            Case Is >= 1099511627776
                DoubleBytes = CDbl(TheSize / 1099511627776) 'TB
                Return FormatNumber(DoubleBytes, 2) & " TB"
            Case 1073741824 To 1099511627775
                DoubleBytes = CDbl(TheSize / 1073741824) 'GB
                Return FormatNumber(DoubleBytes, 2) & " GB"
            Case 1048576 To 1073741823
                DoubleBytes = CDbl(TheSize / 1048576) 'MB
                Return FormatNumber(DoubleBytes, 2) & " MB"
            Case 1024 To 1048575
                DoubleBytes = CDbl(TheSize / 1024) 'KB
                Return FormatNumber(DoubleBytes, 2) & " KB"
            Case 0 To 1023
                DoubleBytes = TheSize ' bytes
                Return FormatNumber(DoubleBytes, 2) & " bytes"
            Case Else
                Return ""
        End Select
    Catch
        Return ""
    End Try
End Function

I made a dll for it.
Then I can import it to my project and I can call it whenever I need to change a byte number to something else "like mb etc"
FormatBytes(GetHDSizeF) "GetHDSizeF is the number of bytes"

Dim DoubleBytes As Double
Default Public Property FormatBytes(ByVal BytesCaller As ULong) As String
    Get
        Try
            Select Case BytesCaller
                Case Is >= 1099511627776
                    DoubleBytes = CDbl(BytesCaller / 1099511627776) 'TB
                    Return FormatNumber(DoubleBytes, 2) & " TB"
                Case 1073741824 To 1099511627775
                    DoubleBytes = CDbl(BytesCaller / 1073741824) 'GB
                    Return FormatNumber(DoubleBytes, 2) & " GB"
                Case 1048576 To 1073741823
                    DoubleBytes = CDbl(BytesCaller / 1048576) 'MB
                    Return FormatNumber(DoubleBytes, 2) & " MB"
                Case 1024 To 1048575
                    DoubleBytes = CDbl(BytesCaller / 1024) 'KB
                    Return FormatNumber(DoubleBytes, 2) & " KB"
                Case 0 To 1023
                    DoubleBytes = BytesCaller ' bytes
                    Return FormatNumber(DoubleBytes, 2) & " bytes"
                Case Else
                    Return ""
            End Select
        Catch
            Return ""
        End Try
    End Get
    Set(value As String)

    End Set
End Property

And if you don't want to make a dll you can use it like a normal function like this.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MsgBox(FormatBytes(2000))
End Sub

Dim DoubleBytes As Double
Public Function FormatBytes(ByVal BytesCaller As ULong) As String

    Try
        Select Case BytesCaller
            Case Is >= 1099511627776
                DoubleBytes = CDbl(BytesCaller / 1099511627776) 'TB
                Return FormatNumber(DoubleBytes, 2) & " TB"
            Case 1073741824 To 1099511627775
                DoubleBytes = CDbl(BytesCaller / 1073741824) 'GB
                Return FormatNumber(DoubleBytes, 2) & " GB"
            Case 1048576 To 1073741823
                DoubleBytes = CDbl(BytesCaller / 1048576) 'MB
                Return FormatNumber(DoubleBytes, 2) & " MB"
            Case 1024 To 1048575
                DoubleBytes = CDbl(BytesCaller / 1024) 'KB
                Return FormatNumber(DoubleBytes, 2) & " KB"
            Case 0 To 1023
                DoubleBytes = BytesCaller ' bytes
                Return FormatNumber(DoubleBytes, 2) & " bytes"
            Case Else
                Return ""
        End Select
    Catch
        Return ""
    End Try

End Function
like image 103
Creator Avatar answered Oct 21 '22 03:10

Creator


You need to divide by 1024 not 1000. 1 KB is not 1000 Bytes, it's 1024 bytes. 1 MB is not 1000 KB, it's 1024 KB, and so on.

Here's a good site to explain all this: http://pc.net/helpcenter/answers/why_1024_bytes

like image 42
rory.ap Avatar answered Oct 21 '22 04:10

rory.ap