Lua 使用lfs遍歷檔案目錄

2021-08-03 17:13:28 字數 1159 閱讀 3554

到公司了,開始學習 lua,看的《

programming in lua

》,大致看了五章,公司培訓出的題。

嘗試用lua指令碼寫乙個檔案系統訪問的**,要求遍歷目錄,輸出檔名、檔案型別(檔案或是目錄)以及檔案的字尾名

果然小白,搞了近3個小時。乙個很重要的工具是lfs,即luafilesystem,幫助我們訪問檔案系統。

主要參考了:lua lfs庫的使用,遞迴獲取子檔案路徑 ,lua 獲取檔名和副檔名,luafilesystem

--lua

require "lfs"

--get filename

function getfilename(str)

local idx = str:match(".+()%.%w+$")

if(idx) then

return str:sub(1, idx-1)

else

return str

endend--get file postfix

function getextension(str)

return str:match(".+%.(%w+)$")

endfunction fun(rootpath)

for entry in lfs.dir(rootpath) do

if entry ~= '.' and entry ~= '..' then

local path = rootpath .. '\\' .. entry

local attr = lfs.attributes(path)

--print(path)

local filename = getfilename(entry)

if attr.mode ~= 'directory' then

local postfix = getextension(entry)

print(filename .. '\t' .. attr.mode .. '\t' .. postfix)

else

print(filename .. '\t' .. attr.mode)

fun(path)

endend

endendfun("e:\\software")

Lua開源庫 lfs的介紹與使用

lua lfs庫 這個庫可以實現平台無關 linux和windows通用 的檔案系統訪問 官網位址 github專案位址 如何配置 5.1以上的lua已經包含了lfs庫,路徑是lua5.1 clibs lfs.dll,我們無需配置直接require lfs 使用即可。提供的功能 lfs的開發提供了功...

使用PHP遍歷檔案目錄與清除目錄中檔案的實現詳解

今天無聊中練習了一下php遍歷檔案目錄的程式,編寫了以下兩個程式,不過質量不是很好,輕拍 1 清除php快取檔案 複製 如下 function read dir dir,file if strpos file,0 strpos file,false return true if strpos fil...

lua 使用lua next 遍歷表

進行下面步驟前先將 table 壓入棧頂 intnindex lua gettop plua 取 table 索引值 lua pushnil plua nil 入棧作為初始 key while 0 lua next plua,nindex 現在棧頂是 table lua next 這個函式的工作過程...