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

  • 相关阅读:
    SSH出现Connection refused错误
    Lisp经典算法
    Arch Linux下韩文重叠显示
    Vim在图形环境下全屏产生黑边
    Vim常用插件安装及配置方法
    Python中Scrapy框架元素选择器XPath的简单实例
    Manjaro下Steam无法启动
    GNOME禁用GDM中night-light功能
    Neovim中提示Error: Required vim compiled with +python
    Manjaro下带供电的USB Hub提示error -71
  • 原文地址:https://www.cnblogs.com/HemJohn/p/5186483.html
Copyright © 2011-2022 走看看