0

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.

Drew
  • 29,895
  • 7
  • 74
  • 104
Sam Price
  • 3
  • 1

1 Answers1

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