-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdealWithFilaName.js
39 lines (37 loc) · 1.34 KB
/
dealWithFilaName.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
const fs = require('fs');
const path = require('path');
// 递归处理当前目录下的所有文件和文件夹
function traverseFolder(folder) {
fs.readdir(folder, (err, files) => {
if (err) {
throw err;
}
files.forEach((file) => {
const fullPath = path.join(folder, file);
fs.stat(fullPath, (err, stats) => {
if (err) {
throw err;
}
if (stats.isDirectory()) {
// 如果是文件夹,则递归处理
traverseFolder(fullPath);
} else if (stats.isFile()) {
// 如果是文件,且文件名匹配 pattern,则修改文件名
const pattern = /^(\d+)、/;
if (pattern.test(file)) {
const oldPath = fullPath;
const newPath = path.join(folder, file.replace(pattern, '$1.'));
fs.rename(oldPath, newPath, (err) => {
if (err) {
throw err;
}
console.log(`${oldPath} -> ${newPath}`);
});
}
}
});
});
});
}
// 开始递归处理当前目录
traverseFolder('.');