GetFileSize ( Bytes ; Precision ; UOM ; Format )
Converts bytes into a labeled file size, appending
Average rating: 4.4 (26 votes) Log in to vote
Julio Toledo - Show more from this author
Automation USA LLC https://www.automationusa.net |
GetFileSize ( Get ( FileSize ) ; "" ; "" ; "s" )
GetFileSize ( Get ( FileSize ) ; 2 ; "metric" ; "long" )
"411 MB" - where Get ( FileSize ) = 430714880 bytes
"1.45 Gigabytes" - where Get ( FileSize ) = 1447866368 bytes
Function definition: (Copy & paste into FileMaker's Edit Custom Function window)
PURPOSE:
• Rounds bytes to the nearest order of magnitude ( up to the Yottabyte level ) at the requested precision [defaults to 0 decimal places] .
• Optionally appends "short-form" ( "MB" ) or "long-form" ( "Megabytes" ) unit label [defaults to
• Provides option to use metric ( 1000 bytes ) or binary (1024 bytes ) unit of measure [defaults to binary].
EXAMPLES:
Input: GetFileSize ( Get ( FileSize ) ; 2 ; "" ; "long" )
Output: "1.35 Gigabytes" - where Get ( FileSize ) = 1447866368 bytes
Input: GetFileSize ( Get ( FileSize ) ; 2 ; "metric" ; "long" )
Output: "1.45 Gigabytes" - where Get ( FileSize ) = 1447866368 bytes
Input: GetFileSize ( Get ( FileSize ) ; "" ; "" ; "s" )
Output: "411 MB" - where Get ( FileSize ) = 430714880 bytes
HISTORY:
2015-10-23 Julio Toledo - Automation USA ( http://www.automationusa.net ). Function originally employed hardcoded binary values and forcibly appended "short form" unit label.
2015-10-31 Julio Toledo - Added "UOM" parameter to optionally use metric standard ( 1000 bytes ).
2015-10-31 Julio Toledo - Added "Format" parameter to optionally append unit label.
2015-10-31 Julio Toledo - Optimized case statement for "Label" variable as per contributions by "Bruce, from Redmond WA". ( Thank you Bruce ).
2015-10-31 Julio Toledo - Optimized "Size" evaluation based on "Magnitude" logarithmic code from Jeremy Bante's "ByteSize" custom function ( http://www.briandunning.com/cf/1447 , https://github.com/jbante/ )
Comments
Bruce, Redmond WA Oct 29, 2015 |
||
Looks like you're not really taking correct advantage of the case statement. No need for the "and <" part where you are evaluating size. Could be more like this: Label = Case ( RawBytes ≥ Yottabyte ; “YB†; RawBytes ≥ Zettabyte ; “ZB†; RawBytes ≥ Exabyte ; “EB†; RawBytes ≥ Petabyte ; “PB†; RawBytes ≥ Terabyte ; “TB†; RawBytes ≥ Gigabyte ; “GB†; RawBytes ≥ Megabyte ; “MB†; RawBytes ≥ Kilobyte ; “KB†; //defaultResult "Bytes" |
||
Julio, Miami, FL Oct 30, 2015 |
||
Thanks Bruce, I take your point, but the reason for the "and <" is not to determine label name, but rather to determine what value "RawBytes" needs to be divided by. |
||
Bruce, Redmond WA Oct 30, 2015 |
||
Sorry that makes no sense. | ||
Bruce, Redmond WA Oct 30, 2015 |
||
But to (perhaps) clarify; my comment was only about that particular section. The complete calc would be like this: /* GetFileSize ( bytes ; precision ) http://www.briandunning.com/cf/1820 2015-10-23 Julio Toledo - Automation USA ( http://www.automationusa.net ) Converts bytes into a labeled file size, appending "short-form" unit symbol up to the Yottabyte level. Can be easily modified to include "long-form" unit name. */ Let ( [ RawBytes = Int ( Bytes ) ; Kilobyte = 1024 ; Megabyte = 1048576 ; Gigabyte = 1073741824 ; Terabyte = 1099511627776 ; Petabyte = 1125899906842624 ; Exabyte = 1152921504606846976 ; Zettabyte = 1180591620717411303424 ; Yottabyte = 1208925819614629174706176 ; Decimals = If ( IsEmpty ( precision ) ; 2 ; Abs ( Int ( precision ) ) ) ; //end If Label = Case ( RawBytes ≥ Yottabyte ; "YB" ; RawBytes ≥ Zettabyte ; "ZB" ; RawBytes ≥ Exabyte ; "EB" ; RawBytes ≥ Petabyte ; "PB" ; RawBytes ≥ Terabyte ; "TB" ; RawBytes ≥ Gigabyte ; "GB" ; RawBytes ≥ Megabyte ; "MB" ; RawBytes ≥ Kilobyte ; "KB" ; //defaultResult "Bytes" ) ; //end Case Size = Case ( Label = "KB" ; Round ( RawBytes/Kilobyte ; Decimals ) ; Label = "MB" ; Round ( RawBytes/Megabyte ; Decimals ) ; Label = "GB" ; Round ( RawBytes/Gigabyte ; Decimals ) ; Label = "TB" ; Round ( RawBytes/Terabyte ; Decimals ) ; Label = "PB" ; Round ( RawBytes/Petabyte ; Decimals ) ; Label = "EB" ; Round ( RawBytes/Exabyte ; Decimals ) ; Label |
||
Bruce, Redmond WA Oct 30, 2015 |
||
Somehow the paste got chopped but hopefully you can figure out that the end of the calc should remain like the bottom section of the existing calc. | ||
Julio, Miami, FL Oct 31, 2015 |
||
Thanks Bruce, based on your feedback, I completely revisited and expanded upon the code. It turns out there already was a similar function written by Jeremy Bante back in 2012 which did not turn up during my original search, so I ended up writing this one instead. In this last revision, I took your input as well as some choice lines of code from Jeremy's function and made this one much better than before, I think. Thanks again, -Julio |
||
Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.