Function: file-name-nondirectory

file-name-nondirectory is a function defined in fileio.c.

Signature

(file-name-nondirectory FILENAME)

Documentation

Return file name FILENAME sans its directory.

For example, in a Unix-syntax file name, this is everything after the last slash, or the entire name if it contains no slash.

Other relevant functions are documented in the file-name group.

View in manual

Shortdoc

;; file-name
(file-name-nondirectory "/tmp/foo")
    => "foo"
  (file-name-nondirectory "/tmp/foo/")
    => ""

Aliases

eshell/basename ff-basename (obsolete since 28.1)

Source Code

// Defined in /usr/src/emacs/src/fileio.c
{
  register const char *beg, *p, *end;
  Lisp_Object handler;

  CHECK_STRING (filename);

  /* If the file name has special constructs in it,
     call the corresponding file name handler.  */
  handler = Ffind_file_name_handler (filename, Qfile_name_nondirectory);
  if (!NILP (handler))
    {
      Lisp_Object handled_name = call2 (handler, Qfile_name_nondirectory,
					filename);
      if (STRINGP (handled_name))
	return handled_name;
      error ("Invalid handler in `file-name-handler-alist'");
    }

  beg = SSDATA (filename);
  end = p = beg + SBYTES (filename);

  while (p != beg && !IS_DIRECTORY_SEP (p[-1])
#ifdef DOS_NT
	 /* only recognize drive specifier at beginning */
	 && !(p[-1] == ':'
	      /* handle the "/:d:foo" case correctly  */
	      && (p == beg + 2 || (p == beg + 4 && IS_DIRECTORY_SEP (*beg))))
#endif
	 )
    p--;

  return make_specified_string (p, -1, end - p, STRING_MULTIBYTE (filename));
}