Solved

format filesize vb6

Posted on 2014-10-07
9
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 48

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 48

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 48

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 48

Expert Comment

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

Featured Post

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!

Question has a verified solution.

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

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

615 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