zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    面向过程,面向对象三大特性
    JDBC连接数据库
    java线程
    ssm
    com组件方面的书籍
    剑雨
    JavaScript为元素动态添加事件之(attachEvent||addEventListener)
    Opacity多浏览器透明度兼容处理
    通过U盘安装Windows 7
    蜀门Online 简单打怪脚本(vbs)
  • 原文地址:https://www.cnblogs.com/HemJohn/p/5186483.html
Copyright © 2011-2022 走看看