1
My code gets writes a file, but needs to check if the file already exists and, if it exists, rename it. It is possible to do this without using the fs.existsSync
?
My current code:
fs.readFile(file.path, function (err, data) {
for (i=2; !fs.existsSync(file.path); i++){
file.name =file.name + i.toString();
file.path = "./public/files/" + file.name;
};
}
fs.writeFile(file.path, data, function (err) {
if (err) {
return console.warn(err);
}
console.log("The file: " + file.name + " was saved as " + file.path);
});