lua跨平台文件夹遍历匹配查找

require"lfs"

--[[Desc:在B路径D文件中下 搜寻A路径下的没用到的C类文件;
并且将没用到的B类文件名称打印出来;
设置好路径拖到lua自带编辑器中即可运行之;]]
--目标所在路径(A)(eg:png所在路径) PNG_FILE_PATH = "E:/Resource/images" --目标所寻路径(B)(eg:lua所在路径) LUA_FILE_PATH = "E:/Resource/scripts" --搜寻目标后缀config(C) SEARCH_TARGET = "%.png" --查找目标后缀config(D) FIND_TARGET = "%.lua" local pairs = pairs function findindir (path, wefind, r_table, intofolder, fileType) for file in lfs.dir(path) do if file ~= "." and file ~= ".." then local f = path..'/'..file local fname = file; if string.find(f, wefind) ~= nil then if fileType == "png" then table.insert(r_table, fname) --print(fname); elseif fileType == "lua" then table.insert(r_table, f) --print(f); end end local attr = lfs.attributes (f) assert (type(attr) == "table") if attr.mode == "directory" and intofolder then findindir (f, wefind, r_table, intofolder, fileType) else -- for name, value in pairs(attr) do -- print (name, value) -- end end end end end function isInIt( file,name ) for line in file:lines() do if isContain(line , name) then return true; end end return false; end function isContain( line , str ) return string.find(line , str); end ----[[ local pngFilePath = PNG_FILE_PATH local input_table_png = {} findindir(pngFilePath, SEARCH_TARGET , input_table_png, true, "png") -- "%.png$" print(#input_table_png); --]] ----[[ local luaFilePath = LUA_FILE_PATH local input_table_lua = {} findindir(luaFilePath, FIND_TARGET , input_table_lua, true, "lua") -- "%.lua$" print(#input_table_lua); --]] ----[[ local count = 0; for _,imageName in pairs(input_table_png) do local flag = 0 for _,fileName in pairs(input_table_lua) do local fileInfo = io.open(fileName,"r") if isInIt(fileInfo , imageName) then flag = 1; break; end end if flag == 0 then print("No Exist Image Name Is: "..imageName) count = count + 1; end end print("The File Count is:"..count); ----]]

lua文件lfs库可参见: http://keplerproject.github.io/luafilesystem/license.html