-->
Page 1 of 2

How can one check if a file exist in LUA?

PostPosted: Mon Dec 01, 2014 9:22 am
by Hans174
Hello,

Is there a command to check if a file exist in LUA ? Something like: file.exist(filename)?

Hans

Re: How can one check if a file exist in LUA?

PostPosted: Mon Dec 01, 2014 9:29 am
by zeroday
file.open("init.lua","r") or file.open("init.lua")
if init.lua not exist, a error shows.
in next release this api will return a nil if the file not exists.

Re: How can one check if a file exist in LUA?

PostPosted: Mon Dec 01, 2014 9:42 am
by gerardwr
Nothing in the API doc, and never seen any workaround yet.

Tried if open has a returning value that you could check: It doesn't :cry:
Code: Select all>  =file.open("init.txt","r")
lua: stdin:1: cannot open file
> file.open("init.txt","r")
lua: stdin:1: cannot open file
>


Only method I see right now is to write a nasty procedure that executes the "file.open" command in a string, catch the return value, and check if this value.

Is a lua challenge, some background here:
http://www.lua.org/pil/8.4.html
http://www.tutorialspoint.com/lua/lua_e ... ndling.htm

If you go on this slippery path, report back if you have any luck, I'm interested.

Re: How can one check if a file exist in LUA?

PostPosted: Mon Dec 01, 2014 9:44 am
by ThomasW
zeroday wrote:file.open("init.lua","r") or file.open("init.lua")
if init.lua not exist, a error shows.
in next release this api will return a nil if the file not exists.


Meanwhile, this should work also:
Code: Select allif file.list()[filename] then ....

(found here: http://dracoblue.net/dev/fast-inarray-o ... mented-lua)

Edit: hey, even better - this returns either nil or the filesize :)

Thomas