const fs = require('fs');
const path = require('path');
function renameFilesInDir(dirPath, oldName, newName) {
fs.readdir(dirPath, (err, files) => {
if (err) {
console.error(err);
return;
}
files.forEach(file => {
const filePath = path.join(dirPath, file);
fs.stat(filePath, (err, stats) => {
if (err) {
console.error(err);
return;
}
if (stats.isDirectory()) {
// Recursively process subdirectories
renameFilesInDir(filePath, oldName, newName);
} else {
if (file === oldName) {
// Rename the file
const newFilePath = path.join(dirPath, newName);
fs.rename(filePath, newFilePath, err => {
if (err) {
console.error(err);
} else {
console.log(`File ${oldName} renamed to ${newName}`);
}
});
}
}
});
});
});
}
// Test code
const dirPath = './test';
const oldName = '';
const newName = '';
renameFilesInDir(dirPath, oldName, newName);