This module provides functions
for analyzing and manipulating filenames. These functions are
designed so that the Erlang code can work on many different
platforms with different filename formats. With filename
is meant all strings that can be used to denote a file. The filename
can be a short relative name like
In Windows, all functions return filenames with forward slashes
only, even if the arguments contain backslashes. To normalize a
filename by removing redundant directory separators, use
The module supports
Functionality in this module generally assumes valid input and
does not necessarily fail on input that does not use a valid
encoding. You can validate the encoding of a filename using
File operations used to accept filenames containing null characters (integer value zero). This caused the name to be truncated at the first null character. Filenames containing null characters inside the filename are now rejected and will cause primitive file operations fail.
Currently null characters at the end of the filename will be accepted by primitive file operations. Such filenames are however still documented as invalid. The implementation will also change in the future and reject such filenames.
Converts a relative
Unix examples:
1> pwd(). "/usr/local" 2> filename:absname("foo"). "/usr/local/foo" 3> filename:absname("../x"). "/usr/local/../x" 4> filename:absname("/"). "/"
Windows examples:
1> pwd(). "D:/usr/local" 2> filename:absname("foo"). "D:/usr/local/foo" 3> filename:absname("../x"). "D:/usr/local/../x" 4> filename:absname("/"). "D:/"
Same as
Joins an absolute directory with a relative filename. Similar to
Equivalent to
Returns a suitable path, or paths, for a given type. If
The options
The path location is intended for transient data files on a local machine.
On Linux:
Respects the os environment variable
1> filename:basedir(user_cache, "my_application", #{os=>linux}). "/home/otptest/.cache/my_application"On Darwin:
1> filename:basedir(user_cache, "my_application", #{os=>darwin}). "/home/otptest/Library/Caches/my_application"On Windows:
1> filename:basedir(user_cache, "My App"). "c:/Users/otptest/AppData/Local/My App/Cache" 2> filename:basedir(user_cache, "My App"). "c:/Users/otptest/AppData/Local/My App/Cache" 3> filename:basedir(user_cache, "My App", #{author=>"Erlang"}). "c:/Users/otptest/AppData/Local/Erlang/My App/Cache" 4> filename:basedir(user_cache, "My App", #{version=>"1.2"}). "c:/Users/otptest/AppData/Local/My App/1.2/Cache" 5> filename:basedir(user_cache, "My App", #{author=>"Erlang",version=>"1.2"}). "c:/Users/otptest/AppData/Local/Erlang/My App/1.2/Cache"
The path location is intended for persistent configuration files.
On Linux:
Respects the os environment variable
2> filename:basedir(user_config, "my_application", #{os=>linux}). "/home/otptest/.config/my_application"On Darwin:
2> filename:basedir(user_config, "my_application", #{os=>darwin}). "/home/otptest/Library/Application Support/my_application"On Windows:
1> filename:basedir(user_config, "My App"). "c:/Users/otptest/AppData/Roaming/My App" 2> filename:basedir(user_config, "My App", #{author=>"Erlang", version=>"1.2"}). "c:/Users/otptest/AppData/Roaming/Erlang/My App/1.2"
The path location is intended for persistent data files.
On Linux:
Respects the os environment variable
3> filename:basedir(user_data, "my_application", #{os=>linux}). "/home/otptest/.local/my_application"On Darwin:
3> filename:basedir(user_data, "my_application", #{os=>darwin}). "/home/otptest/Library/Application Support/my_application"On Windows:
8> filename:basedir(user_data, "My App"). "c:/Users/otptest/AppData/Local/My App" 9> filename:basedir(user_data, "My App",#{author=>"Erlang",version=>"1.2"}). "c:/Users/otptest/AppData/Local/Erlang/My App/1.2"
The path location is intended for transient log files on a local machine.
On Linux:
Respects the os environment variable
4> filename:basedir(user_log, "my_application", #{os=>linux}). "/home/otptest/.cache/my_application/log"On Darwin:
4> filename:basedir(user_log, "my_application", #{os=>darwin}). "/home/otptest/Library/Caches/my_application"On Windows:
12> filename:basedir(user_log, "My App"). "c:/Users/otptest/AppData/Local/My App/Logs" 13> filename:basedir(user_log, "My App",#{author=>"Erlang",version=>"1.2"}). "c:/Users/otptest/AppData/Local/Erlang/My App/1.2/Logs"
On Linux:
Respects the os environment variable
5> filename:basedir(site_data, "my_application", #{os=>linux}). ["/usr/local/share/my_application", "/usr/share/my_application"] 6> os:getenv("XDG_CONFIG_DIRS"). "/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg" 7> filename:basedir(site_config, "my_application", #{os=>linux}). ["/etc/xdg/xdg-ubuntu/my_application", "/usr/share/upstart/xdg/my_application", "/etc/xdg/my_application"] 8> os:unsetenv("XDG_CONFIG_DIRS"). true 9> filename:basedir(site_config, "my_application", #{os=>linux}). ["/etc/xdg/my_application"]On Darwin:
5> filename:basedir(site_config, "my_application", #{os=>darwin}). ["/Library/Application Support/my_application"]
On Linux:
Respects the os environment variable
10> os:getenv("XDG_DATA_DIRS"). "/usr/share/ubuntu:/usr/share/gnome:/usr/local/share/:/usr/share/" 11> filename:basedir(site_data, "my_application", #{os=>linux}). ["/usr/share/ubuntu/my_application", "/usr/share/gnome/my_application", "/usr/local/share/my_application", "/usr/share/my_application"] 12> os:unsetenv("XDG_DATA_DIRS"). true 13> filename:basedir(site_data, "my_application", #{os=>linux}). ["/usr/local/share/my_application", "/usr/share/my_application"]On Darwin:
5> filename:basedir(site_data, "my_application", #{os=>darwin}). ["/Library/Application Support/my_application"]
Returns the last component of
Examples:
5> filename:basename("foo"). "foo" 6> filename:basename("/usr/foo"). "foo" 7> filename:basename("/"). []
Returns the last component of
Examples:
8> filename:basename("~/src/kalle.erl", ".erl"). "kalle" 9> filename:basename("~/src/kalle.beam", ".erl"). "kalle.beam" 10> filename:basename("~/src/kalle.old.erl", ".erl"). "kalle.old" 11> filename:rootname(filename:basename("~/src/kalle.erl")). "kalle" 12> filename:rootname(filename:basename("~/src/kalle.beam")). "kalle"
Returns the directory part of
Examples:
13> filename:dirname("/usr/src/kalle.erl"). "/usr/src" 14> filename:dirname("kalle.erl"). "."
5> filename:dirname("\\usr\\src/kalle.erl"). % Windows "/usr/src"
Returns the file extension of
Examples:
15> filename:extension("foo.erl"). ".erl" 16> filename:extension("beam.src/kalle"). []
Finds the source filename and compiler options for a module.
The result can be fed to
This function is deprecated. Use
If possible, use the
Argument
[{"", ""}, {"ebin", "src"}, {"ebin", "esrc"}]
If the source file is found in the resulting directory, the function
returns that location together with
The function returns
Converts a possibly deep list filename consisting of characters and atoms into the corresponding flat string filename.
Joins a list of filename
The result is "normalized":
Examples:
17> filename:join(["/usr", "local", "bin"]). "/usr/local/bin" 18> filename:join(["a/b///c/"]). "a/b/c"
6> filename:join(["B:a\\b///c/"]). % Windows "b:a/b/c"
Joins two filename components with directory separators.
Equivalent to
Converts
Examples:
19> filename:nativename("/usr/local/bin/"). % Unix "/usr/local/bin"
7> filename:nativename("/usr/local/bin/"). % Windows "\\usr\\local\\bin"
Returns the path type, which is one of the following:
The path name refers to a specific file on a specific volume.
Unix example:
Windows example:
The path name is relative to the current working directory on the current volume.
Example:
The path name is relative to the current working directory on a specified volume, or it is a specific file on the current working volume.
Windows example:
Removes a filename extension.
Examples:
20> filename:rootname("/beam.src/kalle"). /beam.src/kalle" 21> filename:rootname("/beam.src/foo.erl"). "/beam.src/foo" 22> filename:rootname("/beam.src/foo.erl", ".erl"). "/beam.src/foo" 23> filename:rootname("/beam.src/foo.beam", ".erl"). "/beam.src/foo.beam"
Sanitizes the relative path by eliminating ".." and "."
components to protect against directory traversal attacks.
Either returns the sanitized path name, or the atom
The path is not relative.
A ".." component would climb up above the root of the relative path.
Examples:
1> filename:safe_relative_path("dir/sub_dir/.."). "dir" 2> filename:safe_relative_path("dir/.."). [] 3> filename:safe_relative_path("dir/../.."). unsafe 4> filename:safe_relative_path("/abs/path"). unsafe
Returns a list whose elements are the path components of
Examples:
24> filename:split("/usr/local/bin"). ["/","usr","local","bin"] 25> filename:split("foo/bar"). ["foo","bar"] 26> filename:split("a:\\msdev\\include"). ["a:/","msdev","include"]
Validates filename encoding. Returns
Type:
Validates encoding against the
Type:
The encoding will not be interpreted, but null bytes (integer value zero) will be rejected by the validation (even when only present at the end of the filename).
For information on filename encoding see the documentation
of unicode filenames in