lua lfs的使用、介绍

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
inoUnix系统下表示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)
rdevlinux系统下rdev表示设备类型,Windows系统下和dev值相同
access最近一次访问时间
modification最近一次修改时间
change最近一次文件状态修改时间
size文件大小(以字节为单位)
blocks分配给文件的block(Unix Only)
blksize不常用不翻译了:optimal file system I/O blocksize; (Unix only)
- lfs.chdir (path) 
将当前目录改为给定的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



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值