• 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

  • 相关阅读:
    2015 省赛随便写写
    有向图强连通分量
    复杂状态的动态规划
    hdu 3350
    树状DP
    十字链表矩阵
    最优矩阵链乘
    poj 3778
    Poj 3771 hdu 3405
    [2015hdu多校联赛补题]hdu5302 Connect the Graph
  • 原文地址:https://www.cnblogs.com/HemJohn/p/5186483.html
Copyright © 2020-2023  润新知