File and directory size converter

Share your advanced PureBasic knowledge/code with the community.
erion
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Jan 24, 2010 11:12 pm

File and directory size converter

Post by erion »

Hello all,

Here is a procedure that can convert file and directory sizes specified in bytes up to petabytes.
WARNING: you also need the constants, because PB is unable to work with functions specified in CASE statements.

Code: Select all

#pow210=1024
#pow220=1048576
#pow230=1073741824
#pow240=1099511627776
#pow250=1125899906842624
#pow260=1152921504606846976


Procedure.s FileSizeConvert(bytes.q)
Protected output.s

  Select bytes
    Case 0 To #pow210 ; bytes
      output = StrD(bytes,2)+" B"
      Case #pow210 To #pow220 ;kilobytes
      output = StrD(bytes/#pow210,2)+" KB"
    Case #pow220 To #pow230 ;megabytes
      output = StrD(bytes/#pow220,2)+" MB"
    Case #pow230 To #pow240 ;gigabytes
      output = StrD(bytes/#pow230,2)+" GB"
    Case #pow240 To #pow250 ;terabytes
      output = StrD(bytes/#pow240,2)+" TB"
    Case #pow250 To #pow260 ;petabytes
      output = StrD(bytes/#pow250,2)+" PB"
      EndSelect
  
  ProcedureReturn output
    EndProcedure
Erion
To see a world in a grain of sand,
And a heaven in a wild flower,
Hold infinity in the palm of your hand,
And eternity in an hour.

- W. B.

Visit my site, also for PureBasic goodies http://erion.tdrealms.com