2015年4月16日 星期四

[Corona SDK] How to get the files list of some directory?

If we want to get the list of all files in a directory, we can use the LuaFileSystem.
It is convenient for us to show the pictures, especially when we are developing the APP for showing album.
Example:
dir = "/myDir"
for file in lfs.dir(dir) do
    if (file ~= '.' and file ~= '..') then
        print(file)
    end
end
The files and directories will be all listed.
The example below can show the list of all files and directories in current directory, and all files and directories of its sub-directory.
dir = "."
for file in lfs.dir(dir) do
    if (file ~= '.' and file ~= '..') then
     print(file)
        if(lfs.attributes(file,"mode") == "directory") then        
         for subFile in lfs.dir(file) do
          if (subFile ~= '.' and subFile ~= '..') then
          print(file.."/"..subFile)
          end
         end
        end
    end
end

沒有留言:

張貼留言