deleting png files

I want to delete png files in system.TemporaryDirectory.

But “No such file or directory” message is showed.

Please teach resolution!!!

local destDir = system.TemporaryDirectory

local oldfile = system.pathForFile(“temp1.png”)
local newfile = system.pathForFile(“temp1.png”, destDir)

os.rename(oldfile, newfile)

local img = display.newImage(“temp1.png”, destDir, 200, 200)

img:removeSelf()
img = nil

local results, reason = os.remove(“temp1.png”, destDir, 200, 200)

if results then
print( “file removed” )
else
print( reason )
end [import]uid: 133346 topic_id: 27920 reply_id: 327920[/import]

The file has to exist to be removed - files in the temporary directory are cleared when the app is restarted - could that be the issue?

I just tested this by pasting a file into the tmp directory in sandbox and using a timer to try to remove it after 5 seconds (to give me time to add it after refreshing the simulator) and it worked fine.

Peach :slight_smile: [import]uid: 52491 topic_id: 27920 reply_id: 113039[/import]

Please show me your test code. [import]uid: 133346 topic_id: 27920 reply_id: 113119[/import]

[lua]local function delTest()
local destDir = system.TemporaryDirectory
local results, reason = os.remove( system.pathForFile( “test.txt”, destDir ) )

if results then
print( “file removed” )
else
print( “file does not exist”, reason )
end
end

timer.performWithDelay(5000, delTest, 1)[/lua]

All you need to do is make sure you put test.txt in the tmp folder in the project sandbox as soon as it refreshes so it’s there to be removed.

Peach :slight_smile: [import]uid: 52491 topic_id: 27920 reply_id: 113160[/import]

Thank you very much!! I recognize my code’s miss.

But after showing test.png in a display, when trying to remove the file,

“Permission Denied” message was showed.

Please teach resolution again!!

local img = display.newImage(“test.png”, system.TemporaryDirectory, 200, 200)

local function listener()
print( “listener called” )

img:removeSelf()
img = nil

local destDir = system.TemporaryDirectory
local results, reason = os.remove(system.pathForFile( “test.png”, destDir))

if results then
print( “file removed” )
else
print( reason )
end
end

timer.performWithDelay( 5000, listener, 1 ) [import]uid: 133346 topic_id: 27920 reply_id: 113176[/import]

I am not getting any errors with that code - are you getting the error on device or on your computer? If on your computer are you on a PC or Mac? What version of Corona?

Peach :slight_smile: [import]uid: 52491 topic_id: 27920 reply_id: 113195[/import]

Thank you for your reply!!

I’m running the code on a Windows 7 PC.

And version of Corona is 2012.840 (2012.6.5). [import]uid: 133346 topic_id: 27920 reply_id: 113275[/import]

Thanks for that info - I don’t work with Windows personally but I will ask another member of the team first thing tomorrow to see if he can shed some light on this.

Peach :slight_smile: [import]uid: 52491 topic_id: 27920 reply_id: 113304[/import]

Thanks!! [import]uid: 133346 topic_id: 27920 reply_id: 113324[/import]

The Corona Simulator for Windows cannot delete an image file if it is currently being displayed. This is because the file remains open until you delete the display object that is using it… and you cannot delete a file that is currently open. So, you will need to remove the image file’s associated display object first, but that said, the object won’t be removed immediately because Corona won’t delete the object internally until the next render pass. Because of this, you should delete the file via the “applicationExit” system event.

I hope this helps! [import]uid: 32256 topic_id: 27920 reply_id: 113388[/import]

Thank you!! [import]uid: 133346 topic_id: 27920 reply_id: 113566[/import]