

26.9.3 Directory Names

A directory name is a string that must name a directory if it names any file at all. A directory is actually a kind of file, and it has a file name (called the directory file name), which is related to the directory name but is typically not identical. (This is not quite the same as the usual POSIX terminology.) These two names for the same entity are related by a syntactic transformation. On GNU and other POSIX-like systems, this is simple: to obtain a directory name, append a ‘/’ to a directory file name that does not already end in ‘/’. On MS-DOS the relationship is more complicated.

The difference between a directory name and a directory file name is subtle but crucial. When an Emacs variable or function argument is described as being a directory name, a directory file name is not acceptable. When file-name-directory returns a string, that is always a directory name.

The following two functions convert between directory names and directory file names. They do nothing special with environment variable substitutions such as ‘$HOME’, and the constructs ‘~’, ‘.’ and ‘..’.

Function: file-name-as-directory filename

This function returns a string representing filename in a form that the operating system will interpret as the name of a directory (a directory name). On most systems, this means appending a slash to the string (if it does not already end in one).

     ⇒ "~rms/lewis/"
Function: directory-name-p filename

This function returns non-nil if filename ends with a directory separator character. This is the forward slash ‘/’ on GNU and other POSIX-like systems; MS-Windows and MS-DOS recognize both the forward slash and the backslash ‘\’ as directory separators.

Function: directory-file-name dirname

This function returns a string representing dirname in a form that the operating system will interpret as the name of a file (a directory file name). On most systems, this means removing the final directory separators from the string, unless the string consists entirely of directory separators.

(directory-file-name "~lewis/")
     ⇒ "~lewis"
Function: file-name-concat directory &rest components

Concatenate components to directory, inserting a slash before the components if directory or the preceding component didn’t end with a slash.

(file-name-concat "/tmp" "foo")
     ⇒ "/tmp/foo"

A directory or components that are nil or the empty string are ignored—they are filtered out first and do not affect the results in any way.

This is almost the same as using concat, but dirname (and the non-final components) may or may not end with slash characters, and this function will not double those characters.


Function: abbreviate-file-name filename



Next: Functions that Expand Filenames, Previous: Absolute and Relative File Names, Up: File Names   [Contents][Index]