標籤:
在windows下遍曆目錄使用lfs庫:例如遍曆整個目錄下的所有檔案
local lfs = require "lfs"
function findPathName(path)
local fileTbl = {}
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
fileTbl[#fileTbl + 1] = file
end
end
return fileTbl
end
建立目錄:
local function createPath(path)
local tbl = lua_string_split(path,"/")
local str = ""
for k,v in ipairs(tbl) do
if k == 1 then
str = string.format("%s",v)
else
str = string.format("%s/%s",str,v)
end
lfs.mkdir(str)--建立目錄
print(str)
end
end
function lua_string_split(str, split_char)--用於分割字串
local sub_str_tab = {};
while (true) do
local pos = string.find(str, split_char);
if (not pos) then
local size_t = table.getn(sub_str_tab)
table.insert(sub_str_tab,size_t+1,str);
break;
end
local sub_str = string.sub(str, 1, pos - 1);
local size_t = table.getn(sub_str_tab)
table.insert(sub_str_tab,size_t+1,sub_str);
local t = string.len(str);
str = string.sub(str, pos + 1, t);
end
return sub_str_tab;
end
在linxu平台下方法一樣,只是lfs庫換成ls庫
轉載請註明出處:from 部落格園HemJohn
lua在linxu和windows系統下的遍曆目錄的方法