check if file exists

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

check if file exists

Markus Huber
Any ideas how to check the existence of a file?
Or better read the object type e.g.:

   0 doesn't exist
   1 is a file
   2 is a directory
   3 is a link (I don't know the exact name of this type)

Independent of the file access attributes!

I feel that very few further functions are needed to make Lua a great
language for file manipulation. Like read/set access rights and read
directory entries.


--
Markus


Reply | Threaded
Open this post in threaded view
|

Re: check if file exists

Björn De Meyer
Markus Huber wrote:
> 
> Any ideas how to check the existence of a file?
> Or better read the object type e.g.:
> 
>    0 doesn't exist
>    1 is a file
>    2 is a directory
>    3 is a link (I don't know the exact name of this type)
> 
> Independent of the file access attributes!
> 
> I feel that very few further functions are needed to make Lua a great
> language for file manipulation. Like read/set access rights and read
> directory entries.
> 
> --
> Markus

Try opening the file with read only access. 
If that fails the file does not exist.
The other functionality you would like is
beyond the scope of ANSI C, and therefore
you will have to extend Lua yourself with 
these functions. 

-- 
"No one knows true heroes, for they speak not of their greatness." -- 
Daniel Remar.
Björn De Meyer 
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: check if file exists

Basile STARYNKEVITCH
>>>>> "Björn" == Björn De Meyer <[hidden email]> writes:

    Björn> Markus Huber wrote:
    Markus>>  Any ideas how to check the existence of a file?  Or better
    Markus>> read the object type e.g.:
    Markus>> 
    Markus>> 0 doesn't exist 1 is a file 2 is a directory 3 is a link (I
    Markus>> don't know the exact name of this type)
    >> [...]

    Björn> Try opening the file with read only access.  If that fails
    Björn> the file does not exist.  The other functionality you would
    Björn> like is beyond the scope of ANSI C, and therefore you will
    Björn> have to extend Lua yourself with these functions.

On POSIX systems you can use the access(2) and stat(2) system calls.

But please notice that Ansi C knows only (at most, ie not in
standalone or embedded implementations) about files (thru <stdio.h>)
and does *not* know about directories.

Perhaps we could wish a Lua interface to major Posix calls, but this
is a big work (and would make lua quite big).

Regards.
-- 

Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
alias: basile<at>tunes<dot>org 
8, rue de la Faïencerie, 92340 Bourg La Reine, France

Reply | Threaded
Open this post in threaded view
|

Re: check if file exists

Björn De Meyer
Basile STARYNKEVITCH wrote:
> 
> On POSIX systems you can use the access(2) and stat(2) system calls.
> 
> But please notice that Ansi C knows only (at most, ie not in
> standalone or embedded implementations) about files (thru <stdio.h>)
> and does *not* know about directories.
> 
> Perhaps we could wish a Lua interface to major Posix calls, but this
> is a big work (and would make lua quite big).
> 
> Regards.

It already exists, though will probably need to be ported to Lua 5.
Look here for Lua poslib.

http://www.soho-one.com.br/software/poslib/


-- 
"No one knows true heroes, for they speak not of their greatness." -- 
Daniel Remar.
Björn De Meyer 
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: check if file exists

Markus Huber
In reply to this post by Markus Huber
> Björn De Meyer wrote:

> > Markus Huber wrote:

> > Any ideas how to check the existence of a file?

> Try opening the file with read only access.
> If that fails the file does not exist.

Isn't it possible that the file exists with write access only? Or
a protected file without read and write access? The file exists but
opening fails.


--
Markus


Reply | Threaded
Open this post in threaded view
|

Re: check if file exists

Björn De Meyer
Markus Huber wrote:
> 
> > Björn De Meyer wrote:
> 
> > > Markus Huber wrote:
> 
> > > Any ideas how to check the existence of a file?
> 
> > Try opening the file with read only access.
> > If that fails the file does not exist.
> 
> Isn't it possible that the file exists with write access only? Or
> a protected file without read and write access? The file exists but
> opening fails.
> 
> --
> Markus

Yes, that is possible. However, such are the limitations of
ANSI C. And Lua's standard libraries are based upon pure ANSI C.
So either you integrate poslib for lua into your application, 
or you make do with ANSI C. If you really must use the standard
Lua, then maybe the os.execute() function (Lua 5) can help. 
With that, you can execute an arbitrary command, such as, 
say, the Unix utility "test". Write the result of "test" 
to a file or such, and read that back into Lua. 

-- 
"No one knows true heroes, for they speak not of their greatness." -- 
Daniel Remar.
Björn De Meyer 
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: check if file exists

Luiz Henrique de Figueiredo
In reply to this post by Markus Huber
>It already exists, though will probably need to be ported to Lua 5.
>Look here for Lua poslib.
>
>http://www.soho-one.com.br/software/poslib/

See ftp://ftp.tecgraf.puc-rio.br/pub/lhf/poslib.tar.gz for a version for Lua 4.
There's also an old work version  at
		http://www.tecgraf.puc-rio.br/lua/work/poslib.tar.gz
A version for Lua 5 will be available as soon as Lua 5 is released.
--lhf