1.下载node
2.创建.js文件
const fs = require("fs");
const path = require("path");
// 定义要保留的文件名数组
const fileNamesToKeep = [
"文件1","文件2","文件3"
]; // 根据需要修改文件名
// 指定要检查的文件夹路径
const folderPath = "F:/故障"; // 替换为您的文件夹路径
// 读取文件夹内容
fs.readdir(folderPath, (err, files) => {
if (err) {
return console.error(`Unable to scan directory: ${err}`);
}
// 遍历文件夹中的所有文件
files.forEach((file) => {
const fullFilePath = path.join(folderPath, file);
const shouldKeep = fileNamesToKeep.some((name) => file.includes(name));
// 检查该文件是否应该保留
if (!shouldKeep) {
// 如果不在列表中,删除该文件
fs.unlink(fullFilePath, (err) => {
if (err) {
console.error(`Error deleting file: ${fullFilePath}, ${err}`);
} else {
console.log(`Deleted: ${fullFilePath}`);
}
});
} else {
console.log(`Kept: ${fullFilePath}`);
}
});
});