How to get the file size and delete file in Lua?

喜你入骨 提交于 2019-12-23 07:35:35

问题


I have problem in getting the size of the file using Lua. I am creating a function method that if the file size of the file is 743 bytes, then the file will be deleted.

Here is my code :

local getDLFile = function(fileToDL)
            local path = system.pathForFile(fileToDL, system.DocumentsDirectory )
            local myFile = io.open( path, "w+b" ) 
            http.request{ 
                url = "http://www.testfile.com/"..fileToDL, 
                sink = ltn12.sink.file(myFile),
            }

            -- i don't know what is the syntax
            if myFile.size == 743 bytes then
                 myFile.delete
            end             

end

Can anyone can help me about my case?


回答1:


The size is given by myFile:seek("end").

To delete the file, use os.remove(path). But close the file first.




回答2:


Recently Lua File System support was added to Corona! You can get file size using

local lfs = require "lfs"
local size = lfs.attributes (path, "size")

You can have a read here http://keplerproject.github.com/luafilesystem/manual.html#reference

To delete the file use

os.remove(path)


来源:https://stackoverflow.com/questions/10712622/how-to-get-the-file-size-and-delete-file-in-lua

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!