lua没有自己的文件管理 只有读取和写入文件,但是可以通过调用lfs(LuaFileSystem),lfs是一个
用于lua进行文件访问的库,支持lua5.1和lua5.2,并且跨平台
lfs的使用:
"lfs" = { --dump(lfs )
"_COPYRIGHT" = "Copyright (C) 2003 Kepler Project"
"_DESCRIPTION" = "LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution"
"_VERSION" = "LuaFileSystem 1.4.2"
"attributes" = function: 00B3D7A8
"chdir" = function: 00B3D7C8
"currentdir" = function: 00B3D7E8
"dir" = function: 00B3D808
"lock" = function: 00B3D828
"mkdir" = function: 00B3D868
"rmdir" = function: 00B3D888
"setmode" = function: 00B3D8C8
"symlinkattributes" = function: 00B3D8A8
"touch" = function: 00B3D908
"unlock" = function: 00B3D948
}
常用的方法:
lfs.currentdir() --返回当前所在的全路径字符串
lfs.attributes(dir) -- 返回文件的属性table
lfs.dir(path)--用于遍历文件加中的对象
--遍历
function getAllFiles(path, files)
files = files or {}
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
local subPath = path .. "\\" .. file
local attr = lfs.attributes(subPath)
assert(type(attr) == "table")
if attr.mode == "directory" then
getAllFiles(subPath, files)
else
table.insert(files, subPath)
end
end
end
return files
end
--查找
function findInDir (path, wefind, r_table, intofolder)
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
print(file)
local f = path..'/'..file
if string.find(f, wefind) ~= nil then
table.insert(r_table, f)
end
local attr = lfs.attributes(f)
assert(type(attr) == "table")
if attr.mode == "directory" and intofolder then
findInDir(f, wefind, r_table, intofolder)
else
end
end
end
end