Solved

format filesize vb6

Posted on 2014-10-07
9
217 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 45

Expert Comment

by:Martin Liss
Comment Utility
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
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
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
Comment Utility
oki dokes - I dont think its functioning correctly in any event so I will just wait ... thanks for the guidance ...
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 12

Expert Comment

by:jkaios
Comment Utility
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
Comment Utility
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 45

Expert Comment

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

Author Closing Comment

by:dwe0608
Comment Utility
Thanks greatly for the assistance guys

DWE
0
 
LVL 45

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

9 Experts available now in Live!

Get 1:1 Help Now