My code receives write a file, however, it needs to check if the file already exists and, if it exists, rename it. Is it possible to do this without using 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);
});