I will bring myself from here
I use these (but I really check for an error):
require("lfs") -- no function checks for errors. -- you should check for them function isFile(name) if type(name)~="string" then return false end if not isDir(name) then return os.rename(name,name) and true or false -- note that the short evaluation is to -- return false instead of a possible nil end return false end function isFileOrDir(name) if type(name)~="string" then return false end return os.rename(name, name) and true or false end function isDir(name) if type(name)~="string" then return false end local cd = lfs.currentdir() local is = lfs.chdir(name) and true or false lfs.chdir(cd) return is end
os.rename (name1, name2) will rename name1 to name2. Use the same name and nothing should change (except for the badass error). If everything works well, it returns true, otherwise it returns nil and errormessage. If you do not want to use lfs, you cannot distinguish between files and directories without trying to open the file (which is a bit slow but normal).
So without LuaFileSystem
-- no require("lfs") function exists(name) if type(name)~="string" then return false end return os.rename(name,name) and true or false end function isFile(name) if type(name)~="string" then return false end if not exists(name) then return false end local f = io.open(name) if f then f:close() return true end return false end function isDir(name) return (exists(name) and not isFile(name)) end
It looks shorter but takes longer ... Also opening a file is risky
Have fun coding!
tDwtp Feb 07 '14 at 21:09 2014-02-07 21:09
source share