path (ex_stdlib v0.2.0)

View Source

Path manipulation.

This module provides functions for manipulating file system paths in a cross-platform manner. It handles both Unix-style and Windows-style paths appropriately.

Examples:

   AbsPath = path:absname("../relative/path"),
   JoinedPath = path:join(["/home", "user", "documents"]),
   BaseName = path:basename("/path/to/file.txt"),
   DirName = path:dirname("/path/to/file.txt").

Summary

Functions

Returns the absolute name of the given path.

Returns the absolute name of the given path relative to the specified directory.

Returns the basename of the given path.

Returns the basename of the given path with the specified extension removed.

Returns the directory part of the given path.

Expands a path by resolving any environment variables and tildes.

Expands a path relative to the specified directory.

Returns the extension of the given path.

Joins a list of path components into a single path.

Joins two path components into a single path.

Returns the given path relative to the specified directory.

Returns the given path relative to the current working directory.

Returns the path with the extension removed.

Returns the path with the specified extension removed.

Splits a path into its components.

Returns the type of the given path.

Returns a list of files matching the given wildcard pattern.

Returns a list of files matching the given wildcard pattern in the specified directory.

Types

path/0

-type path() :: string() | binary().

path_type/0

-type path_type() :: absolute | relative | volumerelative.

Functions

absname(Path)

-spec absname(path()) -> string().

Returns the absolute name of the given path.

Converts a relative path to an absolute path using the current working directory.

absname(Path, Dir)

-spec absname(path(), path()) -> string().

Returns the absolute name of the given path relative to the specified directory.

Converts a relative path to an absolute path using the given directory as base.

basename(Path)

-spec basename(path()) -> string().

Returns the basename of the given path.

The basename is the last component of the path after the last directory separator.

basename(Path, Ext)

-spec basename(path(), string()) -> string().

Returns the basename of the given path with the specified extension removed.

If the basename ends with the given extension, it is removed.

dirname(Path)

-spec dirname(path()) -> string().

Returns the directory part of the given path.

Returns all components of the path except the last one.

expand(Path)

-spec expand(path()) -> string().

Expands a path by resolving any environment variables and tildes.

Environment variables are specified as $VAR or ${VAR}. Tilde (~) is expanded to the user's home directory.

expand(Path, Dir)

-spec expand(path(), path()) -> string().

Expands a path relative to the specified directory.

First expands the path, then makes it relative to the given directory.

extname(Path)

-spec extname(path()) -> string().

Returns the extension of the given path.

The extension includes the dot (e.g., ".txt"). Returns an empty string if there is no extension.

join(Rest)

-spec join([path()]) -> string().

Joins a list of path components into a single path.

Uses the appropriate path separator for the current platform.

join(Path1, Path2)

-spec join(path(), path()) -> string().

Joins two path components into a single path.

Uses the appropriate path separator for the current platform.

relative_to(Path, Dir)

-spec relative_to(path(), path()) -> string().

Returns the given path relative to the specified directory.

If the path is not under the given directory, returns the path unchanged.

relative_to_cwd(Path)

-spec relative_to_cwd(path()) -> string().

Returns the given path relative to the current working directory.

Converts an absolute path to a relative path from the current directory.

rootname(Path)

-spec rootname(path()) -> string().

Returns the path with the extension removed.

If there is no extension, returns the path unchanged.

rootname(Path, Ext)

-spec rootname(path(), string()) -> string().

Returns the path with the specified extension removed.

Only removes the extension if it matches the given extension.

split(Path)

-spec split(path()) -> [string()].

Splits a path into its components.

Returns a list of path components.

type(Path)

-spec type(path()) -> path_type().

Returns the type of the given path.

Returns absolute for absolute paths, relative for relative paths, and volumerelative for Windows volume-relative paths.

wildcard(Pattern)

-spec wildcard(path()) -> [string()].

Returns a list of files matching the given wildcard pattern.

Uses the current working directory as the base.

wildcard(Pattern, Dir)

-spec wildcard(path(), path()) -> [string()].

Returns a list of files matching the given wildcard pattern in the specified directory.

Uses the given directory as the base for pattern matching.