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()


©2003-2025 BeyondTrust Corporation. All Rights Reserved. Other trademarks identified on this page are owned by their respective owners. BeyondTrust is not a chartered bank or trust company, or depository institution. It is not authorized to accept deposits or trust accounts and is not licensed or regulated by any state or federal banking authority.