Testing for the existence of a file


#1

I am trying to check for a specific file in an open project using atom.project.contains(atom.project.path + "/filename"), where filename is the name of the file. This seems to return true no matter what filename I give it. What is the proper way to check for the existence of a file?

P.S. I know that this approach will not likely work on Windows due to the hardcode “/” path separator.

Thanks,

James


#2
fs = require 'fs'
path = require 'path'

projectPath = atom.project.path
fileExists = fs.existsSync(path.join(projectPath, 'filename'))

#3

Still not sure why contains doesn’t seem to work, but I have determined that I can use atom.project.rootDirectory.getFile("filename").exists() to test for the file.


#4

Just missed your reply before replying myself - thanks.


#5

The documentation states:

Returns whether the given path is inside this project.

Which is probably poorly worded. Just because the path string supplied is under the base path of the project doesn’t mean that a file exists at that path. I’ll see if I can enhance the documentation on that method.


#6

Created a PR with some better documentation for the function in question: