Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

pathForFile function
Started by JoeMakesGames Mar 02 2019 06:49 PM

2 replies to this topic

Best Answer XeduR @Spyric , 02 March 2019 - 07:19 PM

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

JoeMakesGames

[GLOBAL: userInfoPane.html]
JoeMakesGames
  • Observer

  • 7 posts
  • Corona SDK

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


[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 582 posts
  • Corona SDK

  Best Answer

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


[TOPIC: post.html]
#3

JoeMakesGames

[GLOBAL: userInfoPane.html]
JoeMakesGames
  • Observer

  • 7 posts
  • Corona SDK

It seems so simple in hindsight.  Thanks!




[topic_controls]
[/topic_controls]