hyper_w
hyper_w

Reputation: 197

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

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

Upvotes: 6

Views: 2253

Answers (4)

sepp2k
sepp2k

Reputation: 370092

CLISP has a function EXT:PROBE-DIRECTORY, which tells you whether a file exists and is a directory.

Note that this function is specific to CLISP and not standard Common Lisp.

Upvotes: 0

Ken
Ken

Reputation: 326

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")))

Upvotes: 3

John
John

Reputation: 120

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

Upvotes: 1

jondro
jondro

Reputation: 619

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.

Upvotes: 5

Related Questions