lfsUtil.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. --[[
  2. Descripttion:
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-04 11:24:54
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-05 10:11:47
  8. --]]
  9. local lfs = require "lfs"
  10. local root = {}
  11. -- 是否过滤文件
  12. local function l_is_filter_file(file)
  13. return file == "." or file == ".." or file == ".git" or file == ".DS_Store"
  14. end
  15. local function l_get_path_files(path, fileList, upperDir)
  16. path = string.rtrim(path, "/")
  17. if not lfs.attributes(path) then
  18. return
  19. end
  20. fileList = fileList or {}
  21. for file in lfs.dir(path) do
  22. if not l_is_filter_file(file) then
  23. local f = path .. "/" .. file
  24. local attr = lfs.attributes(f)
  25. local filename = file
  26. if upperDir then
  27. filename = string.format("%s/%s", upperDir, filename)
  28. end
  29. -- 目录递归调用
  30. if attr.mode == "directory" then
  31. l_get_path_files(f, fileList, filename)
  32. else
  33. table.insert(fileList, filename)
  34. end
  35. end
  36. end
  37. end
  38. -- 获取目录lua文件列表
  39. function root:get_path_lua_files(path)
  40. local fileList = {}
  41. l_get_path_files(path, fileList)
  42. local list = {}
  43. -- 去除.lua后缀
  44. for _, file in ipairs(fileList) do
  45. local luaFile = file:match("(.+).lua+$")
  46. if luaFile then
  47. table.insert(list, luaFile)
  48. end
  49. end
  50. return list
  51. end
  52. -- 获取目录文件列表
  53. function root:get_path_files(path)
  54. local fileList = {}
  55. l_get_path_files(path, fileList)
  56. return fileList
  57. end
  58. -- 创建文件路径
  59. function root:create_path(path)
  60. local attr = lfs.attributes(path)
  61. if not attr then
  62. lfs.mkdir(path)
  63. end
  64. end
  65. -- 获取文件修改时间
  66. function root:get_file_modification(file)
  67. return lfs.attributes(file, "modification")
  68. end
  69. return root