How to open file just if exists


#1

Is there a way to open a file just if it exists?

I allready tried both ‘AtomEnvironment::open’ and ‘atom.workspace::open’ but didn’t find a way to previously check if a file exists. Has anyone encountered a simillar issue and is able to help?


#2

I’m new to developing for Atom, but in node.js you would use either fs.statSync or fs.accessSync for checking if a file exists, before performing operations on it…


#3

Thanks a lot for the reply! fs.statSync was really helpfull indeed. My solution:

uri=‘C:\YourPath’;
found_a_file=true;
const fs = require(‘fs’);
try {
fs.statSync(uri);
} catch(e) {
found_a_file=false;
}

Maybe not the most elegant way, but it’s working :slight_smile: