[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Check for files
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Check for files

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


Posted by Shaun Biggs   USA  (644 posts)  [Biography] 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.
[Go to top] top

Posted by WillFa   USA  (525 posts)  [Biography] 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.

[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] 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.


4,095 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]