Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 679
gadss Avatar asked May 23 '12 02:05

gadss


People also ask

How to delete files in Lua?

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

What is io flush Lua?

The flush function executes all pending writes to a file. Like the write function, you can call it as a function, io. flush() , to flush the current output file; or as a method, f:flush() , to flush file f . The seek function can be used both to get and to set the current position of a file.

What is Lua file?

A LUA file contains source code written in Lua, a light-weight programming language designed for extending or adding functionality to software applications. The Lua scripting language syntax is simple but supports a large number of data types and operators.


1 Answers

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

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

like image 118
lhf Avatar answered Sep 16 '22 13:09

lhf