pathForFile function

This function is intended to check for the existence of a file in the documents directory.  I know for a fact that the file does not exists as I went in to delete it myself but this function always returns ‘true’.  is pathForFile finding something that I’m unaware of or is my local variable being given a value somehow?  Or am I truly a newb?

Thanks,

local function docFileExists(fileName) local fileNameAt = system.pathForFile( fileName, system.DocumentsDirectory) if fileNameAt then return true else return false end end

Your function will always return true, because you don’t actually check if the file exists. You simply create a path and check if that path, which is a string, exists, and of course it does, because you just created it.

To check if the file itself exists, you need to actually try to open it. This will either succeed or fail depending on whether the file exists at the given path or not.

 

local function docFileExists(fileName) local path = system.pathForFile( fileName, system.DocumentsDirectory) local file = io.open( path ) if file then file:close() -- remember to close the file return true else return false end end

It seems so simple in hindsight.  Thanks!

Your function will always return true, because you don’t actually check if the file exists. You simply create a path and check if that path, which is a string, exists, and of course it does, because you just created it.

To check if the file itself exists, you need to actually try to open it. This will either succeed or fail depending on whether the file exists at the given path or not.

 

local function docFileExists(fileName) local path = system.pathForFile( fileName, system.DocumentsDirectory) local file = io.open( path ) if file then file:close() -- remember to close the file return true else return false end end

It seems so simple in hindsight.  Thanks!