How do you check to see if a file exists?

Post Reply
Darmir
Posts: 226
Joined: Sun May 01, 2011 6:51 pm
Contact:

How do you check to see if a file exists?

Post by Darmir »

I thought I had the correct syntax but it apparently not working.
I have the following:
<lua>
if string.char(getMudletHomeDir():byte()) == "/" then _sep = "/" else _sep = "\\" end
function load_fishing()
local fishing_location = getMudletHomeDir() .. _sep .. "fishing"

if not lfs.attributes(fishing_location) then
echo("Fishing is NOT there")
else
echo("Fising is there")
end
</lua>

Darmir
Posts: 226
Joined: Sun May 01, 2011 6:51 pm
Contact:

Re: How do you check to see if a file exists?

Post by Darmir »

Anybody know how to do this correctly?

User avatar
Oneymus
Posts: 321
Joined: Thu Sep 17, 2009 5:24 am

Re: How do you check to see if a file exists?

Post by Oneymus »

What's the error it's giving?

I would think an easy "if fishing_location then" check would suffice; if the file is there, it's true. If not, it's nil.

Darmir
Posts: 226
Joined: Sun May 01, 2011 6:51 pm
Contact:

Re: How do you check to see if a file exists?

Post by Darmir »

Well the file is there but I get a nil value back. So that isn't the correct syntax.

User avatar
Oneymus
Posts: 321
Joined: Thu Sep 17, 2009 5:24 am

Re: How do you check to see if a file exists?

Post by Oneymus »

According to the documentation for LFS, you should be able to get an error message from attributes. Try...
Code: [show] | [select all] lua
local file, err_msg = lfs.attributes(fishing_location)
if file then
  ...
else
  echo(err_msg)
end
Failing that, I'm afraid I've done all I can do. Haven't used LFS much, and never in Mudlet.

User avatar
Angie
Posts: 51
Joined: Fri May 02, 2014 11:43 pm

Re: How do you check to see if a file exists?

Post by Angie »

I found a way to do this, maybe it will help someone:

Code: Select all

local is_file = io.open(my_path/my_file)
if is_file == nil then
    GUI.Mine:echo("NOPE","white","b10")
else
    GUI.Mine:echo("YAY","yellow","b10")
end
I am not a coder, so if it's a very bad way of doing it, feel free to yell at me.
Angie @ Midnight Sun 2
Alayla @ God Wars 2

Post Reply