Failed to rename temp download file to final download file

I’m trying to make a download manager on my app and everytime that I try to download a file that already exists, I’m getting this error:

Failed to rename temp download file to final download file

I don’t know why because I have a code line to remove the file if it already exists… Here’s my code:

local params = {} params.progress = true local results, reason = os.remove( system.pathForFile( filename, system.DocumentsDirectory ) ) network.download( url, "GET", callback, params, filename, system.DocumentsDirectory)

os.remove is removing the file (already tested with prints that check if file exists) and so, why is the error appearing?

I’m using build 2013.2002 (Graphics 2.0) and testing on windows simulator.

This is because the file already exist in the directory.

Either check whether the file exist before download, or use a unique-filename (UUID).

To check, I usually use :

 -- Output : boolean true = file EXIST, false = false not found! function doesFileExist( fname, path ) local results = false local filePath = system.pathForFile( fname, path ) -- filePath will be nil if file doesn't exist and the path is ResourceDirectory -- if filePath then filePath = io.open( filePath, "r" ) end if filePath then -- print( "Misc : File found -\> " .. fname ) -- Clean up our file handles filePath:close() results = true else -- print( "Misc : File does not exist -\> " .. fname ) end return results end

This is because the file already exist in the directory.

Either check whether the file exist before download, or use a unique-filename (UUID).

To check, I usually use :

 -- Output : boolean true = file EXIST, false = false not found! function doesFileExist( fname, path ) local results = false local filePath = system.pathForFile( fname, path ) -- filePath will be nil if file doesn't exist and the path is ResourceDirectory -- if filePath then filePath = io.open( filePath, "r" ) end if filePath then -- print( "Misc : File found -\> " .. fname ) -- Clean up our file handles filePath:close() results = true else -- print( "Misc : File does not exist -\> " .. fname ) end return results end

Hi,

Did you figure this out? I am having the same problem.

So I figured out what my problem was. I was using a partial path for the filename and the write call to rename the temp file does not want to create sub directories to save the file. It works if the directories actually exist beforehand.

Hi,

Did you figure this out? I am having the same problem.

So I figured out what my problem was. I was using a partial path for the filename and the write call to rename the temp file does not want to create sub directories to save the file. It works if the directories actually exist beforehand.