I'm trying to find the parent directory of a user-given string str
to check if it exists. I can find this easily with (file-name-directory str)
. My issue is I also want to get the parent directory if they pass something with a trailing slash, like "~/Projects/newdir/"
would hopefully get "~/Projects/"
spit out instead of "~/Projects/newdir/"
. I can't seem to find anything like this inside the Emacs documentation.
Asked
Active
Viewed 357 times
0
-
1https://emacs.stackexchange.com/a/9555/2526 – choroba Feb 08 '22 at 20:38
1 Answers
0
I believe you are looking for directory-file-name
:
(file-name-directory "/usr/bin/cc")
==> "/usr/bin/"
(directory-file-name (file-name-directory "/usr/bin/cc"))
==> "/usr/bin"
(file-name-directory (directory-file-name (file-name-directory "/usr/bin/cc")))
==> "/usr/"
(directory-file-name (file-name-directory (directory-file-name (file-name-directory "/usr/bin/cc"))))
==> "/usr"
and file-exists-p
:
(file-exists-p "/usr/bin/cc")
==> t
(file-exists-p "/usr/bin/")
==> t
(file-exists-p "/usr/bin")
==> t
(file-exists-p "/usr/bin/no such file")
==> nil
See also

sds
- 58,617
- 29
- 161
- 278
-
`directory-file-name` does not give the parent directory, but changes an alternative representation of a directory. – Shon Oct 22 '22 at 02:26
-
1