I want to provide additional information to the user during a restart-case
for missing input files.
Is there a portable way to print out the files in the current directory?
I want to provide additional information to the user during a restart-case
for missing input files.
Is there a portable way to print out the files in the current directory?
Please take a look at File System Concepts, specifically at directory
.
E.g.,
(directory "~/*")
to get the list of files in your home, (directory "~/*/")
to get the list of directories in your home, (directory "~/**/*")
to get the list of all files in all subdirectoris in your homeNote that directory
may take implementation-specific arguments like :full
.
If you are using asdf
(you should!), you might want to take a look at uiop
which comes with it.
PS. If your implementation does not support ~
in pathname to refer to the home directory, you can use user-homedir-pathname
instead.
The reference to uiop was the hint I needed. This is what I use now in my test case and which works perfectly for me:
(defun file-is-missing ()
(let* ((cwd (uiop:getcwd))
(files (uiop:directory-files cwd)))
(format t "~&Current directory: ~a~%" cwd)
(dolist (file files)
(format t "~&~3T~a" file))
(format t "~&Provide new file: ")
(list (read-line))))
(defun read-file (file)
(restart-case
(with-open-file (s file :direction :input :if-does-not-exist :error)
(do ((l (read-line s) (read-line s nil 'eof)))
((eq l 'eof) "Reached end of file.")
(format t "~&*** ~A~%" l)))
(New-File (filename)
:interactive file-is-missing
(read-file filename))))