lua 遍历删除_【lua】怎么遍历目录?

Lua 遍历目录处理

2008-10-09 14:54

--使用Lua遍历指定目录,获取所有文件,并使用自定义的函数处理每一个文件

--遍历目录,并对所有的文件进行处理

function get_dir_file(dirpath,func)

--os.execute("dir " .. dirpath .. " /s > temp.txt")

os.execute('dir "' .. dirpath .. '" /s > temp.txt')

io.input("temp.txt")

local dirname = ""

local filename = ""

for line in io.lines() do

local a,b,c

--匹配目录

a,b,c=string.find(line,"^%s*(.+)%s+的目录")

if a then

dirname = c

--print(c)

end

--匹配文件

a,b,c=string.find(line,"^%d%d%d%d%-%d%d%-%d%d%s-%d%d:%d%d%s-[%d%,]+%s+(.+)%s-$")

if a then

filename = c

--print(c)

func(dirname .. "\\" .. filename)

end

--print(line)

end

end

--获取指定的最后一个字符的位置

function get_last_word(all,word)

local b = 0

local last = nil

while true do

local s,e = string.find(all, word, b) -- find 'next' word

if s == nil then

break

else

last = s

end

b = s + string.len(word)

end

return last

end

--可以通过get_last_word获取指定文件的相应路径和相应文件名

filepath = "c:\\windows\\explorer.exe"

pos=get_last_word(filepath,"\\")

dirname=string.sub(filepath,1,pos)

filename=string.sub(filepath,pos+1,-1)

print(dirname,filename)

--使用print函数对C:\Program Files\Internet Explorer文件夹下文件进行处理

--get_dir_file('"C:\\Program Files\\Internet Explorer"',print)

get_dir_file('C:\\Program Files\\Internet Explorer',print)

我问度娘的

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值