Login   /   Register

Convert file size to different size units

Rate this article
     1 votes, average: 4 out of 51 votes, average: 4 out of 51 votes, average: 4 out of 51 votes, average: 4 out of 51 votes, average: 4 out of 5
Loading ... Loading ...
March 31st, 2008 by daniva
Public Sub FileSizeStrings( ByVal fileSize, ByRef sizes )
   Const K_SIZE = 1024
   
   Set sizes = CreateObject( "Scripting.Dictionary" )
   fileSize = CDbl( fileSize )
   sizes.Add "Bytes", Cstr( fileSize ) 
   tmpSize = CDbl( fileSize / K_SIZE )
   sizes.Add "KB", Cstr( tmpSize ) 
   tmpSize = CDbl( tmpSize / K_SIZE )
   sizes.Add "MB", Cstr( tmpSize )
   tmpSize = CDbl( tmpSize / K_SIZE )
   sizes.Add "GB", Cstr( tmpSize ) 
   tmpSize = CDbl( tmpSize / K_SIZE )
   sizes.Add "TB", Cstr( tmpSize ) 
   tmpSize = CDbl( tmpSize / K_SIZE )
   sizes.Add "PB", Cstr( tmpSize )
End Sub

This nice function will convert a size string to a size string representation in various units. the ByRef sizes parameter is converted to a Dictionary in the Sub. then a calculation is performed for every unit offsets of 1K.

Call FileSizeStrings( "5000000000", sizes )
For Each key In sizes.Keys
   Print sizes( key ) & " " & key
Next

The output would be :

clip_image002

Posted in FileSystemObject

Leave a Reply

You must be logged in to post a comment.

This article was viewed 453 times