Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Lisp - how to say if a pathname points to a regular file or a directory?

Tags:

common-lisp

Is it possible to 'stat' a file and find its file type - regular or directory?

like image 263
hyper_w Avatar asked Sep 17 '10 01:09

hyper_w


3 Answers

Read the chapter about a portable pathname library from Peter Seibel's Practical Common Lisp book. It's available for free. It has a function file-exists-p that will return a pathname when the file exists or nil if it doesn't. The returned pathname will be in directory form if it's a directory. He also gives another function for checking if the pathname is indeed in directory form.

BTW the whole book is really worth reading so check it out if you haven't already.

like image 119
jondro Avatar answered Nov 05 '22 09:11

jondro


CL-FAD has a function DIRECTORY-EXISTS-P which, when used in combination with PATHNAME-AS-DIRECTORY canonicalizes the pathname (prevents failure when handed a string like "/path/dir-without-trailing-slash") and achives what you're asking for.

(CL-FAD:DIRECTORY-PATHNAME-P (CL-FAD:PATHNAME-AS-DIRECTORY (PROBE-FILE "/path/missing-slash")))

like image 36
Ken Avatar answered Nov 05 '22 10:11

Ken


I think there are several ways. probe-file followed by checking the returned true name to determine that it has a directory name but not a filename and type should do it. e.g. for a directory

(pathname-name (probe-file filespec))

-> NIL
like image 1
John Avatar answered Nov 05 '22 08:11

John