const fs = require('fs').promises;
async function renameFile() {
const oldPath = 'old-name.txt';
const newPath = 'new-name.txt';
try {
await fs.access(oldPath);
try {
await fs.access(newPath);
console.log('Destination file already exists');
return;
} catch (err) {
}
await fs.rename(oldPath, newPath);
console.log('File renamed successfully');
} catch (err) {
if (err.code === 'ENOENT') {
console.log('Source file does not exist');
} else {
console.error('Error renaming file:', err);
}
}
}
renameFile();