$size

By Relinsquish on Mar 07, 2005

This identifier returns a number converted to a specified file size unit in comma formatted form. This is an enhanced and similar version of $bytes.

/*

Name: $size
Date: March 7, 2005
Description: This identifier returns a number converted to a specified file size unit in comma formatted form.  This is an enhanced and similar version of $bytes.

By: Relinsquish
IRC: irc.undernet.org @ #mIRC.net
E-mail: Relinsquish@mirc.net

Syntax: $size(N,bkmgtpe,bkmgtpe3)

N is the file size.

The bkmgtpe options return the result in the following file size units.

b - bytes
k - kibibytes
m - mebibytes
g - gibibytes
t - tebibytes
p - pebibytes
e - exbibytes

The first bkmgtpe options refer to N's file size unit.
The second bkmgtpe options refer to its converted file size unit.

The .suf property appends the B, KiB, MiB, GiB, TiB, PiB, EiB suffixes to the result.

The 3 option returns a result in 3 digit format.  When the .suf property is used in this situation, it will return the file size unit based on the first bkmgtpe options.

Examples:

$size(123456789,b)       returns 117.74
$size(123456789,b,k)         returns 120,563.27
$size(123456789,g,3)         returns 118
$size(123456789,p).suf       returns 120,563.27EiB
$size(123456789,t,g).suf     returns 126,419,751,936GiB
$size(123456789,b,3).suf     returns 118MiB

Reference: http://www.iec.ch/zone/si/si_bytes.htm

Note: Since $calc has limitations, larger numbers will not be as accurate.

Updates:

March 25, 2005
- Completely recoded the entire alias
- Changed syntax format
- Replaced $istok with $findtok for variables %b and %c
- Fixed inaccurate use of the 3 digit format option
- Rounds to 2 decimal places if the 3 digit format option is not used
- Replaced $calc with $wildtok in final return command for more accurate results
- Replaced $bytes with custom identifier $comma to return a comma formatted number without rounding

March 8, 2005
- Changed from old standard binary units to IEC standard binary units
- Removed unofficial SI units (zebibytes, yobibytes)

*/

alias size {
  if ($isid) {
    if ($1 isnum 0-) {
      var %a = b k m g t p e, %b = $findtok(%a,$2,1,32), %c = $findtok(%a,$3,1,32)
      if (%b) && (!%c) {
        var %d = $len($token($1,1,46)), %e = $int($calc(%d / 3 $iif(3 // %d,-1))))
        return $comma($round($calc($1 $str(/1024,$iif($calc(%b + %e) > 7,$calc(7 - %b),%e))),$iif($3 == 3,$replacex($calc(%d -3* $int($calc(%d / 3))),1,2,2,1,3,0),2))) $+ $iif($prop == suf,$replace($token(%a,$iif($calc(%b + %e) > 7,7,$v1),32),b,B,k,KiB,m,MiB,g,GiB,t,TiB,p,PiB,e,EiB))
      }
      if (%b > %c) {
        var %f = $calc(%b - %c)
      }
      return $comma($round($calc($1 $iif(%f,*,/) $wildtok(1 1024 1048576 1073741824 1099511627776 1125899906842624 1152921504606846976 1180591620717411303424,$str(?,$calc($iif(%f,%f,$calc(%c - %b)) *3+1)),1,32)),2)) $+ $iif($prop == suf && %b && %c,$replace($3,b,B,k,KiB,m,MiB,g,GiB,t,TiB,p,PiB,e,EiB))
    }
    return
  }
}

alias -l comma {
  tokenize 46 $1
  var %x = $1
  while (%x) {
    var %y = $right(%x,3) %y, %x = $left(%x,-3)
  }
  return $replace(%y,$chr(32),$chr(44)) $+ $iif($2,. $+ $2)
}

Comments

Sign in to comment.
splatted   -  Mar 10, 2005

well, for a practical use - there\'s always people asking how to convert file sizes in the snippet request area, eg file list script, mp3 script. Users might look at it and find a solution to their own scripting problem.

 Respond  
Bamargera   -  Mar 08, 2005

Good, but i just cannt find a practical use for it.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.