lua使用io.open跨平台文件夹遍历匹配查找

  -- Desc :实现在LUA_PATH中的lua文件中遍历寻找没用到PNG_PATH路径下的png图片,并将其打印出来。
-- Date :12:49:28 2014-09-04
1 print("Lua Script Start") function getFileName( path )
len = string.len(PNG_PATH);
return string.sub(path, len+) --  remove "/"
end function isInIt( file,name )
--print(file .. " -- " .. name )
for line in io.lines(file) do
if isContain(line , name) then
return true;
end
end
return false;
end function isContain( line , str )
return string.find(line , str);
end PNG_PATH = "user/image"
getPngFileTable = io.popen('find * ' .. PNG_PATH) pngFileTable = {};
for file in getPngFileTable:lines() do
if string.find(file,"%.png$") then
fileName = getFileName(file);
print(fileName)
table.insert(pngFileTable,fileName);
end
end
print("png count is :"..#pngFileTable); LUA_PATH = "user/scripts"
getLuaFileInfo = io.popen('find * ' .. LUA_PATH)
luaFileTable = {};
for file in getLuaFileInfo:lines() do
if string.find(file,"%.lua$") then
--print(file)
table.insert(luaFileTable,file);
end
end local pairs = pairs
for _,name in pairs(pngFileTable) do
flag = ;
for _,file in pairs(luaFileTable) do
if isInIt(file , name) then
flag = ;
break;
end
end
if flag == then
print(name)
end
end print("Lua Script End!") --Desc: lua io.popen ([prog [, mode]])
--Starts program prog in a separated process and returns a file handle that
--you can use to read data from this program (if mode is "r", the default)
--or to write data to this program (if mode is "w").
--This function is system dependent and is not available on all platforms.

注:

1: io.popen()简易说明  Lua中,os.execute可以执行dos命令,但是返回的是系统状态码,默认输出

io.popen()也可以执行dos命令,但是返回一个文件。eg:

  local t = io.popen('svn help')

  local a = t:read("*all")   --a返回一个字符串,内容是svn help的内容

  如果想执行某命令或程序可选os.execute() , 如果还想捕捉该执行结果可用io.popen(),得到的是userdata数据类型;

  eg:复制文件 os.execute("copy" .. originalPath .. "," .. backupPath)

2: io.popen() 是跨平台的,却也跟系统有关,在windows下无法取得访问文件夹的权限,屡次尝试都没成功,liunx和mac下可以;

3: 目前还不得知,使用io.poen()遍历的png图片竟然会 遍历了两边,造成结果是一半为脏数据,记载此文时还在查找原因,不解啊;

上一篇:应对新型“蠕虫”式比特币勒索软件“wannacry”的紧急措施


下一篇:u-boot Makefile Source Test