-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename.js
40 lines (34 loc) · 1.34 KB
/
rename.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const fs = require('fs');
const path = require('path');
// Function to find and rename files recursively
const findAndRenameFiles = (directory) => {
fs.readdir(directory, { withFileTypes: true }, (err, files) => {
if (err) {
console.error(`Error reading directory ${directory}:`, err);
return;
}
files.forEach((file) => {
const fullPath = path.join(directory, file.name);
if (file.isDirectory()) {
// Recurse into subdirectory
findAndRenameFiles(fullPath);
} else if (file.isFile() && file.name.endsWith('_v3.png')) {
// Compute the new file name
const newFileName = file.name.replace('_v3.png', '.png');
const newFullPath = path.join(directory, newFileName);
// Rename the file
fs.rename(fullPath, newFullPath, (err) => {
if (err) {
console.error(`Error renaming file ${fullPath} to ${newFullPath}:`, err);
} else {
console.log(`Renamed ${fullPath} to ${newFullPath}`);
}
});
}
});
});
};
// Directory to search
const directoryToSearch = './';
// Start the search and rename process
findAndRenameFiles(directoryToSearch);