lua在linxu和windows系统下的遍历目录的方法

在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

原文地址:https://www.cnblogs.com/HemJohn/p/5186483.html