Update: Dejay was correct, you have to try and open the file to know for sure if it’s there or not.
Want to know if a file exists before you use it?
Lately, I’ve been wanting something a little more dynamic in my projects for opening various Scenes (think Director, but not). I needed a way of looking to see if a file existed or not before I tried to load it up. Here’s the result:
function fileExists(fileName, base)
assert(fileName, "fileName is missing")
local base = base or system.ResourceDirectory
local filePath = system.pathForFile( fileName, base )
local exists = false
if (filePath) then -- file may exist. won't know until you open it
local fileHandle = io.open( filePath, "r" )
if (fileHandle) then -- nil if no file found
exists = true
io.close(fileHandle)
end
end
return(exists)
end
Typical usage:
if fileExists("myGame.lua") then
-- do something wonderful
end
If the file does not exist, the CoronaSDK library function system.pathForFile() returns nil. It also returns a Warning in the console, which can be safely ignored.
By default it checks your app’s asset directory (system.ResourceDirectory), however, if you’d like to check if a file you created exists you can pass in an alternative base path like so:
if fileExists("some_file.txt", system.DocumentsDirectory) then
-- do something wonderful
end
The three valid options for base path are:
- system.ResourceDirectory
- system.DocumentsDirectory
- system.TemporaryDirectory