Solved

format filesize vb6

Posted on 2014-10-07
9
223 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 46

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 46

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 46

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 46

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now