File Size v1.3.0 FileSize View Source

A file size calculator, parser and formatter.

Usage

You can build your own file size by creating it with a number and a unit using the new/2 function. See the "Supported Units" section for a list of possible unit atoms.

iex> FileSize.new(16, :gb)
#FileSize<"16.0 GB">

Sigil

There is also a sigil defined that you can use to quickly build file sizes from a number and unit symbol. Just use the FileSize module and you are ready to go. See the "Supported Units" section for a list of possible unit symbols.

iex> use FileSize
...>
...> ~F(16 GB)
#FileSize<"16.0 GB">

From File

With from_file/1 it is also possible to retrieve the size of an actual file.

iex> FileSize.from_file("path/to/my/file.txt")
{:ok, #FileSize<"127.3 kB">}

Conversions

You can convert file sizes between different units or unit systems by using the convert/2 function.

Calculations

You can calculate with file sizes. The particular units don't need to be the same for that.

Comparison

For comparison the units of the particular file sizes don't need to be the same.

  • compare/2 - Compares two file sizes and returns a value indicating whether one file size is greater than or less than the other.
  • equals?/2 - Determines whether two file sizes are equal.
  • lt?/2 - Determines whether file size a < b.
  • lteq?/2 - Determines whether file size a <= b.
  • gt?/2 - Determines whether file size a > b.
  • gteq?/2 - Determines whether file size a >= b.

To sort a collection of file sizes from smallest to greatest, you can use lteq?/2 as sort function. To sort descending use gteq?/2.

iex> sizes = [~F(16 GB), ~F(100 Mbit), ~F(27.4 MB), ~F(16 Gbit)]
...> Enum.sort(sizes, &FileSize.lteq?/2)
[#FileSize<"100.0 Mbit">, #FileSize<"27.4 MB">, #FileSize<"16.0 Gbit">, #FileSize<"16.0 GB">]

Supported Units

Bit-based

SI (Système international d'unités)

AtomSymbolNameFactor
:bitbitBits1
:kbitkbitKilobits1000
:mbitMbitMegabits1000^2
:gbitGBitGigabits1000^3
:tbitTBitTerabits1000^4
:pbitPBitPetabits1000^5
:ebitEBitExabits1000^6
:zbitZBitZetabits1000^7
:ybitYBitYottabits1000^8

IEC (International Electrotechnical Commission)

AtomSymbolNameFactor
:bitBitBits1
:kibitKibitKibibits1024
:mibitMibitMebibits1024^2
:gibitGibitGibibits1024^3
:tibitTibitTebibits1024^4
:pibitPibitPebibits1024^5
:eibitEibitExbibits1024^6
:zibitZibitZebibits1024^7
:yibitYibitYobibits1024^8

Byte-based

The most common unit of digital information. A single Byte represents 8 Bits.

SI (Système international d'unités)

AtomSymbolNameFactor
:bBBytes1
:kbkBKilobytes1000
:mbMBMegabytes1000^2
:gbGBGigabytes1000^3
:tbTBTerabytes1000^4
:pbPBPetabytes1000^5
:ebEBExabytes1000^6
:zbZBZetabytes1000^7
:ybYBYottabytes1000^8

IEC (International Electrotechnical Commission)

AtomSymbolNameFactor
:bBBytes1
:kibKiBKibibytes1024
:mibMiBMebibytes1024^2
:gibGiBGibibytes1024^3
:tibTiBTebibytes1024^4
:pibPiBPebibytes1024^5
:eibEiBExbibytes1024^6
:zibZiBZebibytes1024^7
:yibYiBYobibytes1024^8

Link to this section Summary

Types

A type that defines the IEC bit and byte units.

A type that defines the SI bit and byte units.

t()

A type that is a union of the bit and byte types.

A type that is a union of the bit and byte unit types.

A type that represents a unit symbol.

A type that contains the available unit systems.

Functions

Gets the configuration.

Adds two file sizes like add/2 and converts the result to the specified unit.

Converts the given file size to a given unit system.

Converts the given file size to a given unit or unit system.

Determines whether two file sizes are equal.

Builds a new file size from the given number of bits.

Builds a new file size from the given number of bytes.

Determines the size of the file at the given path.

Determines the size of the file at the given path. Raises when the file could not be found.

Determines whether the first file size is greater than the second one.

Determines whether the first file size is less or equal to than the second one.

Determines whether the first file size is less than the second one.

Determines whether the first file size is less or equal to than the second one.

Builds a new file size. Raises when the given unit could not be found.

Converts the given file size to the most appropriate unit.

Subtracts two file sizes like subtract/2 and converts the result to the specified unit.

Link to this section Types

A type that defines the IEC bit and byte units.

A type that defines the SI bit and byte units.

A type that is a union of the bit and byte types.

A type that is a union of the bit and byte unit types.

Link to this type

unit_symbol() View Source
unit_symbol() :: String.t()

A type that represents a unit symbol.

Link to this type

unit_system() View Source
unit_system() :: :iec | :si

A type that contains the available unit systems.

Link to this section Functions

Link to this function

__config__() View Source
__config__() :: Keyword.t()

Gets the configuration.

See FileSize.Calculable.add/2.

Link to this function

add(size, other_size, as_unit_or_unit_system) View Source
add(t(), t(), unit() | {:system, unit_system()}) :: t()

Adds two file sizes like add/2 and converts the result to the specified unit.

Example

iex> FileSize.add(FileSize.new(1, :kb), FileSize.new(2, :kb), :b)
#FileSize<"3000 B">

iex> FileSize.add(FileSize.new(1, :kb), FileSize.new(2, :kb), {:system, :iec})
#FileSize<"2.9296875 KiB">
Link to this function

change_unit_system(size, unit_system) View Source
change_unit_system(t(), unit_system()) :: t()

This function is deprecated. Use convert/2 instead.

Converts the given file size to a given unit system.

Link to this function

compare(size, other_size) View Source

See FileSize.Comparable.compare/2.

Link to this function

convert(size, to_unit_or_unit_system) View Source

Converts the given file size to a given unit or unit system.

Examples

iex> FileSize.convert(FileSize.new(2, :kb), :b)
#FileSize<"2000 B">

iex> FileSize.convert(FileSize.new(2000, :b), :kb)
#FileSize<"2.0 kB">

iex> FileSize.convert(FileSize.new(20, :kb), :kbit)
#FileSize<"160.0 kbit">

iex> FileSize.convert(FileSize.new(2, :kb), {:system, :iec})
#FileSize<"1.953125 KiB">

iex> FileSize.convert(FileSize.new(2, :kib), {:system, :si})
#FileSize<"2.048 kB">

iex> FileSize.convert(FileSize.new(2000, :b), :unknown)
** (FileSize.InvalidUnitError) Invalid unit: :unknown

iex> FileSize.convert(FileSize.new(2, :b), {:system, :unknown})
** (FileSize.InvalidUnitSystemError) Invalid unit system: :unknown
Link to this function

equals?(size, other_size) View Source
equals?(t(), t()) :: boolean()

Determines whether two file sizes are equal.

Examples

iex> FileSize.equals?(FileSize.new(2, :b), FileSize.new(16, :bit))
true

iex> FileSize.equals?(FileSize.new(2, :b), FileSize.new(2, :b))
true

iex> FileSize.equals?(FileSize.new(1, :b), FileSize.new(2, :b))
false
Link to this function

format(size, opts \\ []) View Source

See FileSize.Formatter.format/2.

Link to this function

from_bits(bytes, as_unit_or_unit_system \\ {:system, :si}) View Source
from_bits(integer(), unit() | {:system, unit_system()}) :: t()

Builds a new file size from the given number of bits.

Example

iex> FileSize.from_bits(2000)
#FileSize<"2.0 kbit">

iex> FileSize.from_bits(2000, {:system, :iec})
#FileSize<"1.953125 Kibit">

iex> FileSize.from_bits(16, :b)
#FileSize<"2 B">

iex> FileSize.from_bits(16, :unknown)
** (FileSize.InvalidUnitError) Invalid unit: :unknown
Link to this function

from_bytes(bytes, as_unit_or_unit_system \\ {:system, :si}) View Source
from_bytes(integer(), unit() | {:system, unit_system()}) :: t()

Builds a new file size from the given number of bytes.

Example

iex> FileSize.from_bytes(2000)
#FileSize<"2.0 kB">

iex> FileSize.from_bytes(2000, {:system, :iec})
#FileSize<"1.953125 KiB">

iex> FileSize.from_bytes(2000, :kb)
#FileSize<"2.0 kB">

iex> FileSize.from_bytes(2000, :kbit)
#FileSize<"16.0 kbit">

iex> FileSize.from_bytes(2000, :unknown)
** (FileSize.InvalidUnitError) Invalid unit: :unknown
Link to this function

from_file(path, as_unit_or_unit_system \\ :b) View Source
from_file(Path.t(), unit() | {:system, unit_system()}) ::
  {:ok, t()} | {:error, File.posix()}

Determines the size of the file at the given path.

Examples

iex> FileSize.from_file("path/to/my/file.txt")
{:ok, #FileSize<"133.7 kB">}

iex> FileSize.from_file("path/to/my/file.txt", {:system, :iec})
{:ok, #FileSize<"133.7 KiB">}

iex> FileSize.from_file("path/to/my/file.txt", :mb)
{:ok, #FileSize<"0.13 MB">}

iex> FileSize.from_file("not/existing/file.txt")
{:error, :enoent}
Link to this function

from_file!(path, as_unit_or_unit_system \\ :b) View Source
from_file!(Path.t(), unit() | {:system, unit_system()}) :: t() | no_return()

Determines the size of the file at the given path. Raises when the file could not be found.

Examples

iex> FileSize.from_file!("path/to/my/file.txt")
#FileSize<"133.7 kB">

iex> FileSize.from_file!("path/to/my/file.txt", {:system, :iec})
#FileSize<"133.7 KiB">

iex> FileSize.from_file!("path/to/my/file.txt", :mb)
#FileSize<"0.13 MB">

iex> FileSize.from_file!("not/existing/file.txt")
** (File.Error) could not read file stats "not/existing/file.txt": no such file or directory
Link to this function

gt?(size, other_size) View Source (since 1.2.0)
gt?(t(), t()) :: boolean()

Determines whether the first file size is greater than the second one.

Examples

iex> FileSize.gt?(FileSize.new(2, :b), FileSize.new(1, :b))
true

iex> FileSize.gt?(FileSize.new(1, :b), FileSize.new(2, :b))
false
Link to this function

gteq?(size, other_size) View Source (since 1.2.0)
gteq?(t(), t()) :: boolean()

Determines whether the first file size is less or equal to than the second one.

Examples

iex> FileSize.gteq?(FileSize.new(2, :b), FileSize.new(1, :b))
true

iex> FileSize.gteq?(FileSize.new(1, :b), FileSize.new(1, :b))
true

iex> FileSize.gteq?(FileSize.new(1, :b), FileSize.new(2, :b))
false
Link to this function

lt?(size, other_size) View Source (since 1.2.0)
lt?(t(), t()) :: boolean()

Determines whether the first file size is less than the second one.

Examples

iex> FileSize.lt?(FileSize.new(1, :b), FileSize.new(2, :b))
true

iex> FileSize.lt?(FileSize.new(2, :b), FileSize.new(1, :b))
false
Link to this function

lteq?(size, other_size) View Source (since 1.2.0)
lteq?(t(), t()) :: boolean()

Determines whether the first file size is less or equal to than the second one.

Examples

iex> FileSize.lteq?(FileSize.new(1, :b), FileSize.new(2, :b))
true

iex> FileSize.lteq?(FileSize.new(1, :b), FileSize.new(1, :b))
true

iex> FileSize.lteq?(FileSize.new(2, :b), FileSize.new(1, :b))
false
Link to this function

new(value, unit \\ :b) View Source
new(number(), unit()) :: t() | no_return()

Builds a new file size. Raises when the given unit could not be found.

Examples

iex> FileSize.new(2.5, :mb)
#FileSize<"2.5 MB">

iex> FileSize.new(214, :kib)
#FileSize<"214.0 KiB">

iex> FileSize.new(3, :bit)
#FileSize<"3 bit">

See FileSize.Parser.parse/1.

See FileSize.Parser.parse!/1.

Link to this function

scale(size, unit_system \\ nil) View Source (since 1.1.0)
scale(t(), nil | unit_system()) :: t()

Converts the given file size to the most appropriate unit.

Examples

iex> FileSize.scale(FileSize.new(2000, :b))
#FileSize<"2.0 kB">

iex> FileSize.scale(FileSize.new(2_000_000, :kb))
#FileSize<"2.0 GB">

iex> FileSize.scale(FileSize.new(2_000_000, :kb), :iec)
#FileSize<"1.862645149230957 GiB">
Link to this function

subtract(size, other_size) View Source

See FileSize.Calculable.subtract/2.

Link to this function

subtract(size, other_size, as_unit_or_unit_system) View Source
subtract(t(), t(), unit() | {:system, unit_system()}) :: t()

Subtracts two file sizes like subtract/2 and converts the result to the specified unit.

Example

iex> FileSize.subtract(FileSize.new(2, :b), FileSize.new(6, :bit), :bit)
#FileSize<"10 bit">

iex> FileSize.subtract(FileSize.new(3, :kb), FileSize.new(1, :kb), {:system, :iec})
#FileSize<"1.953125 KiB">