lua跨平台資料夾遍歷匹配查詢

2022-03-04 20:57:52 字數 2775 閱讀 6004

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;

ifstring.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);

endend

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

endend

endend

function

isinit( file,name )

for line in

file:lines() do

if iscontain(line , name) then

return

true

;

endend

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

;

endend

if flag == 0

then

print("

no exist image name is:

"..imagename)

count = count + 1

;

endend

print("

the file count is:

"..count);

----]]

lua檔案lfs庫可參見:

Windows平台 C 遍歷資料夾

include stdafx.h include include include include includeusing namespace std int count 0 注意,lppath最後帶乙個反斜槓 例如 c users kitty void find char lppath char ...

遍歷資料夾

function search path string filename string ball boolean false string varsearchrec tsearchrec begin if findfirst path faanyfile,searchrec 0 then begin...

資料夾遍歷

c 遍歷指定資料夾中的所有檔案 directoryinfo thefolder new directoryinfo folderfullname 遍歷一層資料夾 foreach directoryinfo nextfolder in thefolder.getdirectories 遍歷多層資料夾 ...