File and Path
File and path functions are used to verify, return, and generate information about directories, file paths, names, and file names.
basename
Description
The basename() function returns the file name portion from the provided path. basename actually works by searching the provided string for the rightmost token. A forward slash character (/) delimits tokens. basename ignores any number of trailing slash characters.
For example, given the string /one/two/three, basename returns the rightmost token, which in this case is three.
Given the string /one/two/, basename would ignore the trailing slash and return two.
Syntax
result = basename (path);
Arguments
path Required. Character string containing a file path and file name.
Return values
result contains the rightmost token (that is, the file name) of the supplied character string (that is, the path name). An empty character string ("") is returned if no token is found.
Example
In this example, result contains the file name pblog.txt.
result = basename ("/var/adm/pblog.txt");
dirname
Description
The dirname() function returns the path component of path. dirname() searches the provided string for the rightmost token and returns everything but the rightmost token. Tokens are delimited with the forward slash character (/). dirname ignores all trailing slashes.
For example, given the string /one/two/three, dirname returns everything but the rightmost token. In this example, result contains /one/two/.
Given the string /one/two/three/, dirname ignores the tailing slash and result contains /one/two.
Syntax
result = dirname (path);
Arguments
path: Required. Character string that contains a path and file name
Return values
result contains the contents of path, minus the rightmost token (that is, the file name). If a token is not found, a . is returned.
Example
In the example, result contains the directory /var/adm/.
result = dirname ("/var/adm/pblog.txt");
See also
basename()
Updated 15 days ago