-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelcache.js
62 lines (57 loc) · 1.69 KB
/
delcache.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const fs = require('fs');
const path = require('path');
// 定义要删除的文件夹和文件路径
const foldersToDelete = [
'./docs/2023',
'./docs/2024',
'./docs/zhoukan'
];
const filesToDelete = [
'elog.cache.json'
];
// 检查并删除文件夹
foldersToDelete.forEach(folder => {
if (fs.existsSync(folder)) {
const files = fs.readdirSync(folder);
if (files.length > 0) {
console.log(`文件夹 ${folder} 不为空,正在删除其内容...`);
files.forEach(file => {
const curPath = path.join(folder, file);
if (fs.statSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
}
fs.rmdirSync(folder);
console.log(`文件夹 ${folder} 已被删除`);
} else {
console.log(`文件夹 ${folder} 不存在`);
}
});
// 检查并删除文件
filesToDelete.forEach(file => {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
console.log(`文件 ${file} 已被删除`);
} else {
console.log(`文件 ${file} 不存在`);
}
});
// 递归删除文件夹
function deleteFolderRecursive(folderPath) {
if (fs.existsSync(folderPath)) {
const files = fs.readdirSync(folderPath);
files.forEach(file => {
const curPath = path.join(folderPath, file);
if (fs.statSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(folderPath);
}
}
console.log('清理完成');