How can I see if a file exists?

I am making a highscore saving function. Is there a way to see if the file exits. I want to see if the file is nil and then make it 0 (if it is the first time playing). What am I doing wrong. It always gives me an error saying that

function highscore() local filepath = system.pathForFile("highscoresave.txt", system.DocumentsDirectory) file1 = io.open(filepath,"r") if file1 == nil then io.close(file1) file3 = io.open(filepath,"w") file3:write("0") io.close(file3) end file = io.open(filepath,"r") if scoreNum \> file:read("\*n") then io.close(file) file2 = io.open (filepath,"w") file2:write (scoreNum) io.close(file2) end --file:read("\*n") --lhs = display.newText(saveData,0,0) fileR = io.open(filepath,"r") data = fileR:read("\*a") print (data) lhs = display.newText(data,240,410) io.close (fileR) end

This should work:

function fileExists(myFile, directoryName)         local filePath = system.pathForFile(myFile, directoryName)         local results = false         if filePath == nil then                 return false         else                 local file = io.open(filePath, "r")                 --If the file exists, return true                 if file then                         io.close(file)                     results = true                 end                 return results         end end --check a file if fileExists("myHighScoreFile.json", system.DocumentsDirectory) then     --get your score from the file using io.open else     score = 0 end  

To slightly deviate this topic…

Assume you want to make a table with filenames and their directories…

You can’t use system.DocumentsDirectory or system.ResourceDirectory it seems.

When you print the runtime values out, they result as follows:

Doc Dir =       userdata: 00AF94A1

Res Dir =       userdata: 00AF94A0

How would you specify the directory from a variable?

Thanks!
stu

Hi Stu,

How about using Lua File System (LFS) for this purpose? Here are the relevant guides:

http://docs.coronalabs.com/api/library/lfs/index.html

http://keplerproject.github.io/luafilesystem/manual.html

Brent

This should work:

function fileExists(myFile, directoryName)         local filePath = system.pathForFile(myFile, directoryName)         local results = false         if filePath == nil then                 return false         else                 local file = io.open(filePath, "r")                 --If the file exists, return true                 if file then                         io.close(file)                     results = true                 end                 return results         end end --check a file if fileExists("myHighScoreFile.json", system.DocumentsDirectory) then     --get your score from the file using io.open else     score = 0 end  

To slightly deviate this topic…

Assume you want to make a table with filenames and their directories…

You can’t use system.DocumentsDirectory or system.ResourceDirectory it seems.

When you print the runtime values out, they result as follows:

Doc Dir =       userdata: 00AF94A1

Res Dir =       userdata: 00AF94A0

How would you specify the directory from a variable?

Thanks!
stu

Hi Stu,

How about using Lua File System (LFS) for this purpose? Here are the relevant guides:

http://docs.coronalabs.com/api/library/lfs/index.html

http://keplerproject.github.io/luafilesystem/manual.html

Brent