lua怎么批量删除文件名差不多的文件?

比如删除“Test1.vbs”,"Test2.vbs","Test3.vbs","Test4.vbs"```````"Test50.vbs"

上次不是跟你说了嘛。
由于lua没有内置glob函数,一般这个功能需要其他语言实现。或者你去下载一个名叫
filefind 的模块。他基于lua 5.1
或者使用比较劣质的办法:
-------
function allfiles()
--这个函数返回当前目录所有文件和文件夹列表。
tmpf=os.tmpname()
os.execute("dir /b /a>"..tmpf)
tmp=io.open(tmpf,"r")
allfile={}
line=tmp:read("*l")
while line do
table.insert(allfile,line)
line=tmp:read("*l")
end
tmp:close()
os.remove(tmpf);
return allfile;
end
function glob(pattern)
-- 返回所有匹配列表。
list=allfiles()
res={}
for i,v in ipairs(list) do
if string.match(v,pattern) then
table.insert(res,v)
end
end
return res;
end
function del(list)
for i,v in ipairs(list) do
print ("os.remove(" ..v ..")");
end
end
function main()
-- 输入模式执行删除。
input=io.read()
while input do
del(glob(input));
input=io.read()
end
end
main()
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-27
首先你要使用匹配得出名字差不多的文件
function match(name)
if(string.match("Test%d+%.vbs")) then
return true
else
return false
end
end
其次你要枚举出所有文件名 这个根据你的系统需求
我用的是luaforwindows里的lfs
lfs = require "lfs"
for fileName in lfs.dir(lfs.currentdir()) do
if match(fileName) then
os.execute("del ".. fileName)
end
end
具体没运行过 你可以试试本回答被网友采纳
第2个回答  推荐于2016-06-03
像你提的例子,可以用以下代码:

for i=1, 50 do
os.remove(string.format("Test%d.vbs", i))
end本回答被提问者采纳
第3个回答  2012-08-27
首先你要使用匹配得出名字差不多的文件
function match(name)
if(string.match("Test%d+%.vbs")) then
return true
else
return false
end
end
其次你要枚举出所有文件名 这个根据你的系统需求
我用的是luaforwindows里的lfs
lfs = require "lfs"
for fileName in lfs.dir(lfs.currentdir()) do
if match(fileName) then
os.execute("del ".. fileName)
end
end
具体没运行过 你可以试试本回答被网友采纳
第4个回答  推荐于2016-06-03
像你提的例子,可以用以下代码:

for i=1, 50 do
os.remove(string.format("Test%d.vbs", i))
end本回答被提问者采纳
相似回答
大家正在搜