@@ -7,10 +7,9 @@ We'll see here a handful of functions and libraries to operate on files and dire
77In this chapter, we use mainly
88[ namestrings] ( http://www.lispworks.com/documentation/HyperSpec/Body/19_aa.htm )
99to
10- [ specify filenames] ( http://www.lispworks.com/documentation/HyperSpec/Body/19_.htm ) . The
11- issue of
12- [ pathnames] ( http://www.lispworks.com/documentation/HyperSpec/Body/19_ab.htm )
13- needs to be covered separately.
10+ [ specify filenames] ( http://www.lispworks.com/documentation/HyperSpec/Body/19_.htm ) .
11+ In a recipe or two we also use
12+ [ pathnames] ( http://www.lispworks.com/documentation/HyperSpec/Body/19_ab.htm ) .
1413
1514Many functions will come from UIOP, so we suggest you have a look directly at it:
1615
@@ -105,6 +104,64 @@ UIOP also has `delete-empty-directory`
105104
106105[ cl-fad] [ cl-fad ] has ` (fad:delete-directory-and-files "dirtest") ` .
107106
107+ ### Merging files and directories
108+
109+ Use ` merge-pathnames ` , with one thing to note: if you want to append
110+ directories, the second argument must have a trailing ` / ` .
111+
112+ As always, look at UIOP functions. We have a ` uiop:merge-pathnames* `
113+ equivalent which fixes corner cases.
114+
115+ So, here's how to append a directory to another one:
116+
117+ ~~~ lisp
118+ (merge-pathnames "otherpath" "/home/vince/projects/")
119+ ;; ^^ a trailing / denotes a directory.
120+ ;; => #P"/home/vince/projects/otherpath"
121+ ~~~
122+
123+ Look at the difference: if you don't include a trailing slash to
124+ either paths, ` otherpath ` and ` projects ` are seen as files, so ` otherpath ` is appended to the base directory containing ` projects ` :
125+
126+ ~~~ lisp
127+ (merge-pathnames "otherpath" "/home/vince/projects")
128+ ;; #P"/home/vince/otherpath"
129+ ;; ^^ no "projects", because it was seen as a file.
130+ ~~~
131+
132+ or again, with ` otherpath/ ` (a trailing ` / ` ) but ` projects ` seen as a file:
133+
134+ ~~~ lisp
135+ (merge-pathnames "otherpath/" "/home/vince/projects")
136+ ;; #P"/home/vince/otherpath/projects"
137+ ;; ^^ inserted here
138+ ~~~
139+
140+ ### Get the current working directory (CWD)
141+
142+ Use ` uiop/os:getcwd ` :
143+
144+ ~~~ lisp
145+ (uiop/os:getcwd)
146+ ;; #P"/home/vince/projects/cl-cookbook/"
147+ ;; ^ with a trailing slash, useful for merge-pathnames
148+ ~~~
149+
150+ ### Get the current directory relative to a Lisp project
151+
152+ Use ` asdf:system-relative-pathname system path ` .
153+
154+ Say you are working inside ` mysystem ` . It has an ASDF system
155+ declaration, the system is loaded in your Lisp image. This ASDF file
156+ is somewhere on your filesystem and you want the path to ` src/web/ ` . Do this:
157+
158+ ~~~ lisp
159+ (asdf:system-relative-pathname "mysystem" "src/web/")
160+ ;; => #P"/home/vince/projects/mysystem/src/web/"
161+ ~~~
162+
163+ This will work on another user's machine, where the system sources are located in another location.
164+
108165
109166### Opening a file
110167
0 commit comments