需要对一些层级的各种目录进行一些特定操作。可以把不同目录下放入不同的命名标志文件。lua遍历这些目录,发现了对应的名字的文件,就对这个目录进行特定操作。
下面的代码是目录下遇到_0_auto_generate_list.txt这个文件就自动生成列表操作,遇到_0_auto_generate_description.txt就生成描述操作。
local targetDirectory = "../Web/"
local function generateCatagoryList(path)
print("generating catagory list in "..path)
...
end
local function generateDescription(path)
print("generating description in "..path)
...
end
--遍历目录
local lfs = require("lfs")
--目录末尾要带'/'
local function traverseDirectory(path)
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
local filepath = path .. file
local mode = lfs.attributes(filepath, "mode")
if mode == "file" then
if file == "_0_auto_generate_list.txt" then
generateCatagoryList(path)
elseif file == "_0_auto_generate_description.txt" then
generateDescription(path)
end
elseif mode == "directory" then
traverseDirectory(filepath..'/')
end
end
end
end
traverseDirectory(targetDirectory)