f



How to check if file is a regular file (not directory)?

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 (?).
0
tlikonen (176)
3/21/2010 6:04:20 AM
comp.lang.lisp 16861 articles. 5 followers. Post Follow

3 Replies
834 Views

Similar Articles

[PageSpeed] 16

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


0
vanekl
3/21/2010 7:22:30 AM
On Mar 21, 2:04=A0am, Teemu Likonen <tliko...@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:

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

> 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.h=
tml

[2] http://weitz.de/cl-fad/
0
Pillsy
3/21/2010 12:33:43 PM
* 2010-03-21 05:33 (-0700), Pillsy wrote:

> On Mar 21, 2:04´┐Żam, Teemu Likonen <tliko...@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.
0
Teemu
3/21/2010 4:43:29 PM
Reply: