到公司了,开始学习 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 end end --get file postfix function getExtension(str) return str:match(".+%.(%w+)$") end function 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) end end end end fun("E:\\software")
