Lua uses Io. Open cross-platform folder traversal to search

Source: Internet
Author: User
-- Desc: traverse the Lua file in lua_path to find and print the PNG image that is not used in the png_path path.
-- 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) do11 if iscontain (line, name) then 12 Return true; 13 end14 end15 return false; 16 end17 18 function iscontain (line, STR) 19 Return string. find (line, STR); 20 End21 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, "2.16.png $") then28 filename = getfilename (File); 29 print (filename) 30 table. insert (pngfiletable, filename); 31 end32 end33 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 $ ") then40 -- print (File) 41 table. insert (luafiletable, file); 42 end43 end44 45 local pairs = pairs46 for _, name in pairs (pngfiletable) do47 flag = 0; 48 for _, file in pairs (luafiletable) do49 if isinit (file, name) then 50 flag = 1; 51 break; 52 end53 end54 if flag = 0 then 55 print (name) 56 end57 en D58 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.

Note:

1: Io. popen (): In Lua, OS .exe cute can execute the DOS command, but the system status code is returned. The default output is

Io. popen () can also execute the doscommand, but returns a file. Eg:

Local T = Io. popen ('svn help ')

Local A = T: Read ("* All") -- A returns a string containing the content of SVN help.

If you want to execute a command or program, you can select OS .exe cute (). If you want to capture the execution result, you can use Io. popen () to obtain the userdata data type;

Eg: copy the file OS .exe cute ("copy"... originalpath... ","... backuppath)

2: Io. popen () is cross-platform, but it is also related to the system. In Windows, you cannot obtain the permission to access the folder. It has failed many attempts. It can be used in liunx or Mac;

3: I/O. the PNG image traversed by poen () will traverse both sides, resulting in half of the result being dirty data. I still find the reason for recording this article;

 

Lua uses Io. Open cross-platform folder traversal to search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.