-- Desc :实现在LUA_PATH中的lua文件中遍历寻找没用到PNG_PATH路径下的png图片,并将其打印出来。
-- Date :12:49:28 2014-09-04
1 print("Lua Script Start") 2 3 function getFileName( path ) 4 len = string.len(PNG_PATH); 5 return string.sub(path, len+2) -- remove "/" 6 end 7 8 function isInIt( file,name ) 9 --print(file .. " -- " .. name ) 10 for line in io.lines(file) do 11 if isContain(line , name) then 12 return true; 13 end 14 end 15 return false; 16 end 17 18 function isContain( line , str ) 19 return string.find(line , str); 20 end 21 22 PNG_PATH = "user/image" 23 getPngFileTable = io.popen('find * ' .. PNG_PATH) 24 25 pngFileTable = {}; 26 for file in getPngFileTable:lines() do 27 if string.find(file,"%.png$") then 28 fileName = getFileName(file); 29 print(fileName) 30 table.insert(pngFileTable,fileName); 31 end 32 end 33 print("png count is :"..#pngFileTable); 34 35 LUA_PATH = "user/scripts" 36 getLuaFileInfo = io.popen('find * ' .. LUA_PATH) 37 luaFileTable = {}; 38 for file in getLuaFileInfo:lines() do 39 if string.find(file,"%.lua$") then 40 --print(file) 41 table.insert(luaFileTable,file); 42 end 43 end 44 45 local pairs = pairs 46 for _,name in pairs(pngFileTable) do 47 flag = 0; 48 for _,file in pairs(luaFileTable) do 49 if isInIt(file , name) then 50 flag = 1; 51 break; 52 end 53 end 54 if flag == 0 then 55 print(name) 56 end 57 end 58 59 print("Lua Script End!") 60 61 --Desc: lua io.popen ([prog [, mode]]) 62 --Starts program prog in a separated process and returns a file handle that 63 --you can use to read data from this program (if mode is "r", the default) 64 --or to write data to this program (if mode is "w"). 65 --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图片竟然会 遍历了两边,造成结果是一半为脏数据,记载此文时还在查找原因,不解啊;