• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
  -- 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图片竟然会 遍历了两边,造成结果是一半为脏数据,记载此文时还在查找原因,不解啊;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
lua元表以及元方法发布时间:2022-07-22
下一篇:
理解LUA中的多重继承发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap