tags:

views:

193

answers:

4
+2  Q: 

Convert KB to MB?

Hello,

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

+3  A: 

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

Beth
To downvoters: Actually, he's right. `kb` stands for `kilobyte` which means 1,000 bytes...while `kib` stands for `kibibyte`, wich means 1,024 bytes. That's an ISO-Norm.
Bobby
Except that many people hardly follow the new naming scheme and you usually have to expect both a 1000-base as well as a 1024-base conversion.. Plus some people make a difference between `kb` and `KB`..
poke
@poke: Sadly this isn't a new scheme...it's roots are ranging back to 1996.
Bobby
+3  A: 

(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)

Kedar Soparkar
thanks for that - should have made myself more clear! I know how to convert KB to MB - the problem is how do I incorporate that into my code? Do I add another variable for strMax as / 1024?
lara400
brilliant! thanks for this!!
lara400
A: 

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

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

KevinDTimm
A: 
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
dbasnett