Solved

format filesize vb6

Posted on 2014-10-07
9
235 Views
Last Modified: 2014-10-08
Hi experts

The following function returns a formatted string of the passed in value (which is a filesize in bytes). How can I modify this function to return a formatted string if the size is a gigbyte or greater?

Public Function FileSize(ByVal lSize As Currency) As String 
    If lSize < 1024 Then
        FileSize = lSize & " Bytes"
    ElseIf lSize < 1048576 Then
        FileSize = Format(lSize / 1024#, "#,##0.00") & " KB"
    ElseIf lSize > 1048576 Then
        FileSize = Format(lSize / 1024# ^ 2, "###,##0.00") & " MB"
    End If
End Function

Open in new window


MTIA

DWE
0
Comment
Question by:dwe0608
9 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40367469
Public Function FileSize(ByVal lSize As Currency) As String
    Select Case lSize
        Case Is < 1024
            FileSize = lSize & " Bytes"
        Case Is < 1048576
            FileSize = Format(lSize / 1024#, "#,##0.00") & " KB"
        Case Is < 1048577
            FileSize = Format(lSize / 1024# ^ 2, "###,##0.00") & " MB"
        Case Else
            FileSize = Format(lSize / 1024# ^ 3, "###,###,##0.00") & " GB"
    End Select
End Function

Open in new window

0
 
LVL 1

Author Comment

by:dwe0608
ID: 40367513
Hi Martin,

your modification to the function always returned the size in GB ... I modded the function as follows and seems to work ... can you confirm that you agree with the change?

Public Function FileSize(ByVal lSize As Currency) As String
    Select Case lSize
        Case Is < 1024
            FileSize = lSize & " Bytes"
        Case Is < 1048576
            FileSize = Format(lSize / 1024#, "#,##0.00") & " KB"
        Case Is < 1000000000 ' > 1048577
            FileSize = Format(lSize / 1024# ^ 2, "###,##0.00") & " MB"
        Case Else
            FileSize = Format(lSize / 1024# ^ 3, "###,###,##0.00") & " GB"
    End Select
End Function

Open in new window

0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40367528
I'm not at home so I can't test it but I will later. BTW I don't think that the lengthy format strings with the multiple sets of ### are necessary.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:dwe0608
ID: 40367531
oki dokes - I dont think its functioning correctly in any event so I will just wait ... thanks for the guidance ...
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40367538
A different version that is more scaleable.  Just change the Double datatype to Currency if desired.

Public Function SizeToString(ByVal dBytes As Double) As String

   Dim sWork As String
   Dim vWork As Variant
   Dim lWork As Long
   Dim dTemp As Double
   
   sWork = Format(dBytes, "###,#")
   vWork = Split(sWork, ",")
   lWork = UBound(vWork)
   
   Select Case lWork
     Case 0      '// Hundred
       sWork = " bytes"
       vWork = dBytes
     Case 1      '// Thousand
       dTemp = dBytes / 1024
       sWork = " KB"
       vWork = Round(dTemp, 2)
     Case 2      '// Million
       dTemp = dBytes / (1024 ^ 2)
       sWork = " MB"
       vWork = Round(dTemp, 2)
     Case 3      '// Billion
       dTemp = dBytes / (1024 ^ 3)
       sWork = " GB"
       vWork = Round(dTemp, 2)
     Case 4      '// Trillion
       dTemp = dBytes / (1024 ^ 4)
       sWork = " TB"
       vWork = Round(dTemp, 2)
   End Select
   
   If InStr(vWork, ".") Then
      SizeToString = Format(CDbl(vWork), "#0.0") & sWork
   Else
      SizeToString = vWork & sWork
   End If

End Function

Open in new window

0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 total points
ID: 40367718
This is how I would write it. It can then be extended easily to TB etc if required. No output should be > 1024, so extra formatting is not needed.

Public Function FileSize(ByVal lSize As Currency) As String
    Select Case lSize
        Case Is < 1024
            FileSize = Format(lSize , "#,##0.00") &  " Bytes"
        Case Is < 1024 ^ 2
            FileSize = Format(lSize / 1024, "#,##0.00") & " KB"
        Case Is < 1024 ^ 3
            FileSize = Format(lSize / (1024 ^ 2), "#,##0.00") & " MB"
        Case Else
            FileSize = Format(lSize / (1024 ^ 3), "#,##0.00") & " GB"
    End Select
End Function

Open in new window

0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40368426
ChloesDad's solution is a good improvement on my code.
0
 
LVL 1

Author Closing Comment

by:dwe0608
ID: 40369966
Thanks greatly for the assistance guys

DWE
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40369986
I would have thought that you'd split the points between ChloesDad and me.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FileUp - Classic ASP 5 24
Call a function within the ASP code 4 26
Replace value 2 27
Asp response.write to clients 6 33
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question