2012-05-23 93 views
10

我有使用Lua獲取文件大小的問題。我正在創建一個函數方法,如果文件的文件大小是743 bytes,那麼文件將被刪除。如何獲取文件大小並刪除Lua中的文件?

這裏是我的代碼:

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 

能有人能幫助我瞭解我的情況?

回答

18

尺寸由myFile:seek("end")給出。

要刪除文件,請使用os.remove(path)。但先關閉文件。

+0

感謝LHF你的答案獲取文件的大小,我嘗試'打印(MYFILE :seek(「end」))''但是我的上帝這些錯誤在我的電暈'運行時錯誤 ... ne \我的文檔\唱歌偶像決賽\ freeplay.lua:258:嘗試到 使用封閉文件 堆棧回溯: [C]:? [C]:在函數'seek' ... ne \ my documents \歌唱偶像決賽\ freeplay.lua:258:在函數中 'getDLFile' ... ne \ my d' – gadss

+1

@gadss,it似乎你正在關閉該文件,然後在尋求什麼時候應該關閉它。 – lhf

+0

請看這裏http://www.lua.org/pil/21.3.html – frgtv10