I'm walking a directory tree upwards from default-directory
in a loop, so I need a way to say "take the last component off this path that ends in a slash and return a result that ends in a slash". The closest built-in thing I found is (file-name-directory)
and it's not a great fit. I ended up writing this:
(defun pop-dir (dir)
"Pops a trailing directory off DIR to return the remainder of the path."
(concat "/"
;; there's probably some better way to do this but ¯\_(ツ)_/¯
(string-join (reverse (cdr (reverse (split-string dir "/" t)))) "/")
"/"))
There has to be a better way; what is it?
pop-dir
should return the parent directory of its argument? If so, you can write(file-name-directory (directory-file-name FILENAME))
for any givenFILENAME
, regardless of whether it names a file or directory.concat
. String functions really know nothing about files, directories, or their names.