lua在linxu和windows系統下的遍曆目錄的方法

來源:互聯網
上載者:User

標籤:

 

 

在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系統下的遍曆目錄的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.