lara400
lara400

Reputation: 4736

Convert KB to MB?

SEE BOTTOM OF THIS POST FOR UPDATE ON THIS PLEASE.

I have the below code that searches through directories and displays the largest file in the directory. the problem is that it displays it in KB - how on earth do I convert it to MB? The file size comes out way too large so want easier reading - thanks for the help:

Private Sub btnGetMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetMax.Click
    ClearList()

    Dim dblSize As Integer = 0
    Dim dblMax As Integer = 0
    Dim strMax As String = ""

    Dim objFileInfo As System.IO.FileInfo

    For Each strFile As String In My.Computer.FileSystem.GetFiles("c:\temp", FileIO.SearchOption.SearchAllSubDirectories)

        objFileInfo = My.Computer.FileSystem.GetFileInfo(strFile)
        /*whats the size of the files?*/
        dblSize = objFileInfo.Length

        If dblSize > dblMax Then
            dblMax = dblSize
            strMax = objFileInfo.FullName
        End If
    Next

    MessageBox.Show("Largest file in .Net folder is " & vbCrLf &
                    strMax & vbCrLf &
                    dblMax.ToString("N0"))
End Sub

SHOULD HAVE MADE MYSELF MORE CLEAR! I KNOW HOW TO CONVERT KB TO MB BUT NO IDEA HOW I INCORPORATE THAT INTO MY CODE - DO I ADD ANOTHER VARIABLE FOR STRMAX AS /1024.....EXCEPT I ALREADY HAVE STRMAX VARIABLE.....STILL VERY MUCH A BEGINNER GUYS.

I know how to convert KB to MB - the problem is how do I incorporate that into my code? Do I add another variable

Upvotes: 4

Views: 6222

Answers (6)

dotomu
dotomu

Reputation: 13

I found a good answer here from user NeverHopeless and adjusted the code to follow the IEC standards like omar's ByteSize NuGet:

Public Module SizeExtension
    Public Enum MetricUnits
        [Byte] ' Needs to be escaped as Byte is a reserved keyword
        kB
        MB
        GB
        TB
        PB
        EB
        ZB
        YB
        RB
        QB
    End Enum

    Public Enum BinaryUnits
        [Byte]
        KiB
        MiB
        GiB
        TiB
        PiB
        EiB
        ZiB
        YiB
    End Enum

    <System.Runtime.CompilerServices.Extension> 
    Public Function ToSize(value As Int64, sourceUnit As MetricUnits, targetUnit As MetricUnits) As Double
        Return value / Math.Pow(1000, CType(targetUnit - sourceUnit, Int64))
    End Function

    <System.Runtime.CompilerServices.Extension> 
    Public Function ToSize(value As Int64, sourceUnit As BinaryUnits, targetUnit As BinaryUnits) As Double
        Return value / Math.Pow(1024, CType(targetUnit - sourceUnit, Int64))
    End Function
End Module

Now you can do something like this:

' Bytes to MegaBytes:
Dim sizeInMB As Double = sizeInBytes.ToSize(SizeExtension.MetricUnits.Byte, SizeExtension.MetricUnits.MB)
' MegaBytes to Bytes:
Dim sizeInBytes As Double = sizeInMB.ToSize(SizeExtension.MetricUnits.MB, SizeExtension.MetricUnits.Byte)

For clarity, I shared this code already here as an answer.

Upvotes: 0

Joe Koen
Joe Koen

Reputation: 11

Put this at the top of the document.

Dim imin As Integer 'Bytes
Dim imax As Integer 'Bytes
Dim imin1 As Integer 'Kb
Dim imax1 As Integer 'kb

Then try to rename some stuff to match yours.

Private Sub WC_DownloadProgressChanged(sender As Object, e As System.Net.DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged

    Try
        imin = e.BytesReceived / 1024 'Bytes converted to KB
        imax = e.TotalBytesToReceive / 1024 'Bytes converted to KB
        imin1 = imin / 1024 'Converts to MB
        imax1 = imax / 2014 'Converts to MB
    Catch ex As Exception

    End Try

    Try
        ProgressBar1.Maximum = e.TotalBytesToReceive
        ProgressBar1.Value = e.BytesReceived
        Label1.Text = imin1 & "MB of " & imax1 & "MB"
    Catch ex As Exception

    End Try
End Sub

This will convert it into MB which is mostly used for downloads.

Since people like the adv way, this is the easy/simple way ;)

Upvotes: 0

dbasnett
dbasnett

Reputation: 11773

Enum xByte As Long
    kilo = 1024L
    mega = 1024L * kilo
    giga = 1024L * mega
    tera = 1024L * giga
End Enum


Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button1.Click

    For x As Integer = 2 To 4
        Debug.WriteLine("")
        Dim d As Double = 1024 ^ x

        Debug.WriteLine(String.Format("{0} bytes ", d.ToString("n0")))

        Debug.WriteLine(String.Format("{0} KB ", (d / xByte.kilo).ToString("n3")))
        Debug.WriteLine(String.Format("{0} MB ", (d / xByte.mega).ToString("n3")))
        Debug.WriteLine(String.Format("{0} GB ", (d / xByte.giga).ToString("n3")))
        Debug.WriteLine(String.Format("{0} TB ", (d / xByte.tera).ToString("n3")))
    Next

End Sub

Upvotes: 0

Beth
Beth

Reputation: 9607

divide by 1000?

re: HOW I INCORPORATE THAT INTO MY CODE - DO I ADD ANOTHER VARIABLE

you can add another variable if you want, it will be easier to do debugging. Just give it a new name. You can also do the division inline (see @KevinDTimm 's solution).

Upvotes: 3

user191776
user191776

Reputation:

(Sorry for the previous answer with 1024, a mistaken assumption)

To your question of converting from kB to MB, you can surely assume by SI standard:

1 MB = 1000 kB

Ergo, divide by 1000.

For the unconvinced, I encourage you to read this.

Since software like Microsoft Windows expresses storage quantities in multiples of 1024 bytes, change your code to:

  dblMax = dblMax/(1024*1024)  

  MessageBox.Show("Largest file in .Net folder is " & vbCrLf &
  strMax & vbCrLf &
  dblMax.ToString("N0"))

(since you are printing dblMax & your file size is in bytes, not kB)

Upvotes: 5

KevinDTimm
KevinDTimm

Reputation: 14376

I would just say strMax = objFileInfo.FullName & ' ' & (dblSize / 1024) & 'MB'

(sorry about the syntax, I haven't done VB in > 10 years)

Upvotes: 0

Related Questions