Lua lfs库
这个库可以实现平台无关(Linux和Windows通用)的文件系统访问
lfs开源库存在路径 项目->frameworks->runtime-src->Classes->quick-src->lua_extensions->filesystem
如何配置:
5.1以上的lua已经包含了lfs库,路径是Lua5.1\clibs\lfs.dll,我们无需配置直接require “lfs”使用即可。
提供的功能:
lfs的开发提供了功能的介绍:官方手册
下面给出精简了内容的中文版(方便不喜欢看英文版的同学):
- lfs.attributes (filepath [, aname])
返回这个path的属性table,如果filepath是nil则会出错并打印错误信息,属性列表如下所示:
mode属性是字符串,其他属性都是数组。
属性 | 描述 |
---|---|
dev | 不常用不翻译了:on Unix systems, this represents the device that the inode resides on. On Windows systems, represents the drive number of the disk containing the file |
ino | Unix系统下表示inode数目,Windows系统下无意义 |
mode | 这个字符串表示关联的保护模式,值可能是file、directory、link、socket、named pipe、char device、block device or other |
nlink | 文件上的硬链接数 |
uid | 目录的user-id(Unix only, always 0 on Windows) |
gid | 用户的group-id(Unix only, always 0 on Windows) |
rdev | linux系统下rdev表示设备类型,Windows系统下和dev值相同 |
access | 最近一次访问时间 |
modification | 最近一次修改时间 |
change | 最近一次文件状态修改时间 |
size | 文件大小(以字节为单位) |
blocks | 分配给文件的block(Unix Only) |
blksize | 不常用不翻译了:optimal file system I/O blocksize; (Unix only) |
将当前目录改为给定的path
- lfs.currentdir ()
获取当前目录
- lfs.dir (path)
Lua遍历目录下的所有入口,每次迭代都返回值为入口名的字符串
- lfs.lock (filehandle, mode[, start[, length]])
锁定一个文件或这文件的部分内容
- lfs.mkdir (dirname)
创建一个目录
- lfs.rmdir (dirname)
移除一个已存在的目录
- lfs.setmode (file, mode)
设置文件的写入模式,mode字符串可以是binary或text
- lfs.symlinkattributes (filepath [, aname])
比lfs.attributes多了the link itself (not the file it refers to)信息,其他都和lfs.attribute一样
- lfs.touch (filepath [, atime [, mtime]])
设置上一次使用和修改文件的时间值
- lfs.unlock (filehandle[, start[, length]])
解锁文件或解锁文件的部分内容
小实例 传入一个根目录路径,递归获取该路径子目录的所有文件全路径:
local lfs = require "lfs"
local allFilePath = {}
function main()
-- 打印lfs库的版本
PrintLfsVersion()
-- 打印当前目录地址
PrintCurrrentDir()
-- 打印当前目录的下一层子目录地址
local rootPath = lfs.currentdir()
PrintDirChildren(rootPath)
-- 递归打印当前目录下面所有层级的子目录并将文件地址保存到表中
GetAllFiles(rootPath)
PrintTable(allFilePath)
end
function PrintLfsVersion( ... )
print(lfs._VERSION)
end
function PrintCurrrentDir( ... )
local rootPath = lfs.currentdir()
print(rootPath)
end
function PrintDirChildren(rootPath)
for entry in lfs.dir(rootPath) do
if entry~='.' and entry~='..' then
local path = rootPath.."\\"..entry
local attr = lfs.attributes(path)
assert(type(attr)=="table") --如果获取不到属性表则报错
-- PrintTable(attr)
if(attr.mode == "directory") then
print("Dir:",path)
elseif attr.mode=="file" then
print("File:",path)
end
end
end
end
function GetAllFiles(rootPath)
for entry in lfs.dir(rootPath) do
if entry~='.' and entry~='..' then
local path = rootPath.."\\"..entry
local attr = lfs.attributes(path)
assert(type(attr)=="table") --如果获取不到属性表则报错
-- PrintTable(attr)
if(attr.mode == "directory") then
-- print("Dir:",path)
GetAllFiles(path) --自调用遍历子目录
elseif attr.mode=="file" then
-- print(attr.mode,path)
table.insert(allFilePath,path)
end
end
end
end
function PrintTable( tbl , level, filteDefault)
local msg = ""
filteDefault = filteDefault or true --默认过滤关键字(DeleteMe, _class_type)
level = level or 1
local indent_str = ""
for i = 1, level do
indent_str = indent_str.." "
end
print(indent_str .. "{")
for k,v in pairs(tbl) do
if filteDefault then
if k ~= "_class_type" and k ~= "DeleteMe" then
local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v))
print(item_str)
if type(v) == "table" then
PrintTable(v, level + 1)
end
end
else
local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v))
print(item_str)
if type(v) == "table" then
PrintTable(v, level + 1)
end
end
end
print(indent_str .. "}")
end
main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86