Function: file-name-split

file-name-split is a byte-compiled function defined in files.el.gz.

Signature

(file-name-split FILENAME)

Documentation

Return a list of all the components of FILENAME.

On most systems, this will be true:

  (equal (string-join (file-name-split filename) "/") filename)

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

View in manual

Probably introduced at or before Emacs version 29.1.

Shortdoc

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

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun file-name-split (filename)
  "Return a list of all the components of FILENAME.
On most systems, this will be true:

  (equal (string-join (file-name-split filename) \"/\") filename)"
  (let ((components nil))
    ;; If this is a directory file name, then we have a null file name
    ;; at the end.
    (when (directory-name-p filename)
      (push "" components)
      (setq filename (directory-file-name filename)))
    ;; Loop, chopping off components.
    (while (length> filename 0)
      (push (file-name-nondirectory filename) components)
      (let ((dir (file-name-directory filename)))
        (setq filename (and dir (directory-file-name dir)))
        ;; If there's nothing left to peel off, we're at the root and
        ;; we can stop.
        (when (and dir (equal dir filename))
          (push (if (equal dir "") ""
                  ;; On Windows, the first component might be "c:" or
                  ;; the like.
                  (substring dir 0 -1))
                components)
          (setq filename nil))))
    components))