How to translate (make-pathname :directory '(:absolute :home "directoryiwant") into absolute path
Asked Answered
C

2

4

I want to be able to translate a certain directory in my homedirectory on any OS to the actual absolute path on that OS e.g. (make-pathname :directory '(:absolute :home "directoryiwant") should be translated to "/home/weirdusername/directoryiwant" on a unixlike system.

What would be the function of choice to do that? As

(directory-namestring 
      (make-pathname :directory '(:absolute :home "directoryiwant"))
> "~/"

does not actually do the deal.

Carpous answered 20/5, 2013 at 18:37 Comment(0)
W
9

If you need something relative to your home directory, the Common Lisp functions user-homedir-pathname and merge-pathnames can help you:

CL-USER> (merge-pathnames 
          (make-pathname
           :directory '(:relative "directoryyouwant"))
          (user-homedir-pathname))
#P"/home/username/directoryyouwant/"

The namestring functions (e.g., namestring, directory-namestring) work on this pathname as expected:

CL-USER> (directory-namestring
          (merge-pathnames 
           (make-pathname
            :directory '(:relative "directoryyouwant"))
           (user-homedir-pathname)))
"/home/username/directoryyouwant/"
Wileywilfong answered 20/5, 2013 at 19:3 Comment(0)
V
4
CL-USER > (make-pathname :directory (append (pathname-directory
                                              (user-homedir-pathname))
                                            (list "directoryiwant"))
                         :defaults (user-homedir-pathname))

#P"/Users/joswig/directoryiwant/"

The function NAMESTRING returns it as a string.

CL-USER > (namestring #P"/Users/joswig/directoryiwant/")
"/Users/joswig/directoryiwant/"
Vivisection answered 20/5, 2013 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.