Solved

format filesize vb6

Posted on 2014-10-07
9
242 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiflying 2 Input Text On a Table 7 35
Two decimal 5 42
SQL Server 2012 to SQL Server 2016 24 54
Userform to show a range in excel. 3 32
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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

737 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