From: Teemu Likonen on
I'm unsure how to check if a filename is regular file (and not a
directory, for example). I mean similar check that Unix command "test
-f" does. Currently I'm using this:


(defun file-exists-and-is-regular-file-p (path)
"Return t if file exists and is a regular file.
Return nil otherwise."
(and (probe-file path)
(pathname-name (truename path))
t))


But it can only tell files from directories. Actually this is sufficient
for me now but I wonder if there are better ways (?).
From: vanekl on
Teemu Likonen wrote:
> I'm unsure how to check if a filename is regular file (and not a
> directory, for example). I mean similar check that Unix command "test
> -f" does. Currently I'm using this:
>
>
> (defun file-exists-and-is-regular-file-p (path)
> "Return t if file exists and is a regular file.
> Return nil otherwise."
> (and (probe-file path)
> (pathname-name (truename path))
> t))
>
>
> But it can only tell files from directories. Actually this is
> sufficient for me now but I wonder if there are better ways (?).

Not sure what you're concerned about. It's pretty solid.
Taking out 't' would help. It's more lispy without it, looks better, gives
the same results, and doesn't destroy information (you're able to return the
file name, which is more information than just a 't' you're currently
returning).


From: Pillsy on
On Mar 21, 2:04 am, Teemu Likonen <tliko...(a)iki.fi> wrote:
> I'm unsure how to check if a filename is regular file (and not a
> directory, for example). I mean similar check that Unix command "test
> -f" does. Currently I'm using this:

>     (defun file-exists-and-is-regular-file-p (path)
>       "Return t if file exists and is a regular file.
>     Return nil otherwise."
>       (and (probe-file path)
>            (pathname-name (truename path))
>            t))

> But it can only tell files from directories. Actually this is sufficient
> for me now but I wonder if there are better ways (?).

There's a good discussion of this in Chapter 15 of /Practical Common
Lisp/[1]. The main challenge is doing this portably, since the various
pathname functions are pretty loosely specified. For instance, calling
PROBE-FILE on a directory will signal an error under CLISP. If you
don't care about portability, then what you have is probably fine.

If you just want a library, check out CL-FAD[2], which provides an
enhanced version of the code from PCL.

HTH,
Pillsy

[1] http://www.gigamonkeys.com/book/practical-a-portable-pathname-library.html

[2] http://weitz.de/cl-fad/
From: Teemu Likonen on
* 2010-03-21 05:33 (-0700), Pillsy wrote:

> On Mar 21, 2:04�am, Teemu Likonen <tliko...(a)iki.fi> wrote:
>> I'm unsure how to check if a filename is regular file (and not a
>> directory, for example). I mean similar check that Unix command "test
>> -f" does.

> There's a good discussion of this in Chapter 15 of /Practical Common
> Lisp/[1]. The main challenge is doing this portably, since the various
> pathname functions are pretty loosely specified. For instance, calling
> PROBE-FILE on a directory will signal an error under CLISP. If you
> don't care about portability, then what you have is probably fine.
>
> If you just want a library, check out CL-FAD[2], which provides an
> enhanced version of the code from PCL.

Sigh. Such a simple task and still a portability layer is needed.

But cl-fad works. Thank you.