11,706,159 members (63,050 online)
Tip/Trick

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

, 5 Apr 2013 CPOL 19.6K 9
 Rate this:
Convert filesize (bytes) according to highest possible size scale (KB, MB, GB, TB, PB).

## 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)```

## You may also be interested in...

 First Prev Next
 Definitions of the prefixes John Brett2-Aug-12 5:47 John Brett 2-Aug-12 5:47
 Re: Definitions of the prefixes rctaubert2-Aug-12 9:11 rctaubert 2-Aug-12 9:11
 Re: Definitions of the prefixes John Brett2-Aug-12 21:59 John Brett 2-Aug-12 21:59
 Much simpler C# Version jlopez7882-Aug-12 4:13 jlopez788 2-Aug-12 4:13
 Re: Much simpler C# Version rctaubert2-Aug-12 9:21 rctaubert 2-Aug-12 9:21
 Re: Much simpler C# Version Kim Nordmo5-Apr-13 5:04 Kim Nordmo 5-Apr-13 5:04
 Re: Much simpler C# Version giammin5-Apr-13 5:06 giammin 5-Apr-13 5:06
 Re: Much simpler C# Version George Swan7-Apr-13 11:18 George Swan 7-Apr-13 11:18
 Re: Much simpler C# Version John Brett7-Apr-13 21:32 John Brett 7-Apr-13 21:32
 Re: Much simpler C# Version George Swan7-Apr-13 22:13 George Swan 7-Apr-13 22:13
 Last Visit: 31-Dec-99 18:00     Last Update: 29-Aug-15 20:24 Refresh 1