Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Check for files

Check for files

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Tue 28 Jul 2009 02:31 AM (UTC)
Message
Is there any way to check for a file before trying to use dofile() on it? Or, probably more usefully, is there any way to trap the error for when there is no file and do something instead of displaying something like the following?

Run-time error
World: Aardwolf - Balaam
Immediate execution
cannot open asdf.lua: No such file or directory
stack traceback:
        [C]: in function 'dofile'
        [string "Command line"]:1: in main chunk


It is much easier to fight for one's ideals than to live up to them.
Top

Posted by WillFa   USA  (525 posts)  Bio
Date Reply #1 on Tue 28 Jul 2009 02:50 AM (UTC)
Message
Ooooh! I came up against this one 2 or 3 weeks ago!

if utils.readdir("C:\\path\\to\\file.lua") then
-- do stuff.
end

Using ReadDir (not re-add ir, there is no infra red support ;) ) with a fully qualified file path will either return the file name, or nil if not found.

Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #2 on Tue 28 Jul 2009 03:03 AM (UTC)

Amended on Tue 28 Jul 2009 03:04 AM (UTC) by Nick Gammon

Message
If you check out the help for dofile, it says:


Using dofile is the same as:


function dofile (filename)
  local f = assert (loadfile (filename))
  return f ()
end -- dofile



Thus, if you do loadfile, you will get back nil if it fails. If not, you call the resulting function (see under loadfile).

So, basically:


function my_dofile (filename)
  local f = loadfile (filename)
  if not f then
    print "sigh -- foiled again!"
    return nil
  end -- can't load it
  return f ()
end -- my_dofile


This is more comprehensive than merely testing for the presence of the file, this checks it can be loaded (ie. it is valid Lua code).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,452 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.