# filename

### MODULE

filename

### MODULE SUMMARY

File Name Manipulation Functions

### DESCRIPTION

The module `filename` provides a number of useful functions for analyzing and manipulating file names. These functions are designed so that the Erlang code can work on many different platforms with different formats for file names. With file name is meant all strings that can be used to denote a file. They can be short relative names like `foo.erl`, very long absolute name which include a drive designator and directory names like `D:\usr/local\bin\erl/lib\tools\foo.erl`, or any variations in between.

In Windows, all functions return file names with forward slashes only, even if the arguments contain back slashes. Use the `join/1` function to normalize a file name by removing redundant directory separators.

### EXPORTS

`absname(Filename) -> Absname`

Types:

`Filename = string() |[string()] | atom()`
`Absname = string()`

Converts a relative `Filename` and returns an absolute name. No attempt is made to create the shortest absolute name, because this can give incorrect results on file systems which allow links.

Examples include:

    Assume (for UNIX) current directory "/usr/local"
    Assume (for WIN32) current directory "D:/usr/local"

    (for UNIX):  absname("foo") -> "/usr/local/foo"
    (for WIN32): absname("foo") -> "D:/usr/local/foo"
    (for UNIX):  absname("../x") -> "/usr/local/../x"
    (for WIN32): absname("../x") -> "D:/usr/local/../x"
    (for UNIX):  absname("/") -> "/"
    (for WIN32): absname("/") -> "D:/"

`absname(Filename, Directory) -> Absname`

Types:

`Filename = string() |[string()] | atom()`
`Directory = string()`
`Absname = string()`

This function works like `absname/1`, except that the directory to which the file name should be made relative is given explicitly in the `Directory` argument.

`basename(Filename)`

Types:

`Filename = string() |[string()] | atom()`

Returns the part of the `Filename` after the last directory separator, or the `Filename` itself if it has no separators.

Examples include:

    basename("foo") -> "foo"
    basename("/usr/foo") -> "foo"
    basename("/") -> []

`basename(Filename,Ext) -> string()`

Types:

`Filename = Ext = string() | [string()] | atom()`

Returns the last component of `Filename` with the extension `Ext` stripped. Use this function if you want to to remove an extension which might, or might not, be there. Use `rootname(basename(Filename))` if you want to remove an extension that exists, but you are not sure which one it is.

Examples include:

    basename("~/src/kalle.erl", ".erl") -> "kalle"
    basename("~/src/kalle.beam", ".erl") -> "kalle.beam"
    basename("~/src/kalle.old.erl", ".erl") -> "kalle.old"
    rootname(basename("~/src/kalle.erl")) -> "kalle"
    rootname(basename("~/src/kalle.beam")) -> "kalle"

`dirname(Filename) -> string()`

Types:

`Filename = string() | [string()] | atom()`

Returns the directory part of `Filename`.

Examples include:

    dirname("/usr/src/kalle.erl") -> "/usr/src"
    dirname("kalle.erl") -> "."
    On Win32:
    filename:dirname("\\usr\\src/kalle.erl") -> "/usr/src"

`extension(Filename) -> string() | []`

Types:

`Filename = string() | [string()] | atom()`

Given a file name string `Filename`, this function returns the file extension including the period. Returns an empty list if there is no extension.

Examples include:

    extension("foo.erl") -> ".erl"
    extension("beam.src/kalle") -> []

`join(Components) -> string()`

Types:

`Components = [string()]`

Joins a list of file name `Components` with directory separators. If one of the elements in the `Components` list includes an absolute path, for example "/xxx", the preceding elements, if any, are removed from the result.

The result of the `join` function is "normalized":

- There are no redundant directory separators.
- In Windows, all directory separators are forward slashes and the drive letter is in lower case.

Examples include:

    join("/usr/local", "bin") -> "/usr/local/bin"
    join(["/usr", "local", "bin"]) -> "/usr/local/bin"
    join(["a/b///c/"] -> "a/b/c"
    join(["B:a\\b///c/"] -> "b:a/b/c"  % On Windows only

`join(Name1, Name2) -> string()`

Types:

`Name1 = Name2 = string()`

Joins two file name components with directory separators. Equivalent to `join([Name1,Name2]).`

`nativename(Path) -> string()`

Types:

`Path = string()`

Converts a filename in `Path` to a form accepted by the command shell and native applications on the current platform. On Windows, forward slashes will be converted to backward slashes. On all platforms, the name will be normalized as done by `join/1`.

Example:

    (on UNIX)  filename:nativename("/usr/local/bin/") -> "/usr/local/bin"
    (on Win32) filename:nativename("/usr/local/bin/") -> "\\usr\\local\\bin"

`pathtype(Path) -> absolute | relative | volumerelative`

Returns one of `absolute`, `relative`, or `volumerelative`.

`absolute`
  The path name refers to a specific file on a specific volume.
  Examples include:

      on Unix
      /usr/local/bin/
      on Windows
      D:/usr/local/bin

`relative`
  The path name is relative to the current working directory on the current volume.
  Example:

      foo/bar, ../src

`volumerelative`
  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.
  Examples include:

      In Windows
      D:bar.erl, /bar/foo.erl
      /temp

`rootname(Filename) -> string()`
`rootname(Filename, Ext) -> string()`

Types:

`Filename = Ext = string() | [string()] | atom()`

`rootname/1` returns all characters in `Filename`, except the extension.

`rootname/2` works as `rootname/1`, except that the extension is removed only if it is `Ext`.

Examples include:

    rootname("/beam.src/kalle") -> "/beam.src/kalle"
    rootname("/beam.src/foo.erl") -> "/beam.src/foo"
    rootname("/beam.src/foo.erl",".erl") -> "/beam.src/foo"
    rootname("/beam.src/foo.beam",".erl") -> "/beam.src/foo.beam"

`split(Filename) -> Components`

Types:

`Filename = string() | [string()] | atom()`
`Components = [string()]`

Returns a list whose elements are the path components of `Filename`.

Examples include:

    split("/usr/local/bin") -> ["/", "usr", "local", "bin"]
    split("foo/bar") -> ["foo", "bar"]
    split("a:\\msdev\\include") -> ["a:/", "msdev", "include"]

`find_src(Module) -> {SourceFile, Options}`
`find_src(Module, Rules) -> {SourceFile, Options}`

Types:

`Module = atom() | string()`
`SourceFile = string()`
`Options = [CompilerOption]`
`CompilerOption = {i, string()} | {outdir, string()} | {d, atom()}`

Finds the source file name and compilation options for a compiled module. The result can be fed to `compile:file/2` in order to compile the file again.

The Module argument, which can be a string or an atom, specifies either the module name or the path to the source code, with or without the ".erl" extension. In either case, the module must be known by the code manager, i.e. `code:which/1` must succeed.

Rules describe how the source directory is found, when the object code directory is known. Each rule is of the form `{BinSuffix, SourceSuffix}` and is interpreted as follows: If the end of the directory name where the object is located matches `BinSuffix`, then the suffix of the directory name is replaced by `SourceSuffix`. If the source file is found in the resulting directory, then the function returns that location together with `Options`. Otherwise, the next rule is tried, and so on.

The function returns `{SourceFile, Options}`. `SourceFile` is the absolute path to the source file without the ".erl" extension. `Options` include the options which are necessary to compile the file with `compile:file/2`, but excludes options such as `report` or `verbose` which do not change the way code is generated. The paths in the `{outdir, Path}` and `{i, Path}` options are guaranteed to be absolute.

### AUTHORS

Björn Gustavsson - support@erlang.ericsson.se

stdlib 1.10.1
Copyright © 1991-2002 Ericsson Utvecklings AB