const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
function getMd5(filePath) {
const fileData = fs.readFileSync(filePath);
return crypto.createHash('md5').update(fileData).digest('hex');
}
function findFiles(dirPath, fileList = []) {
const files = fs.readdirSync(dirPath);
files.forEach((file) => {
const filePath = path.join(dirPath, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
findFiles(filePath, fileList);
} else if (stats.isFile()) {
fileList.push(filePath);
}
});
return fileList;
}
function compareMd5AndDeleteDuplicates(fileList) {
const md5Map = new Map();
const deleteList = [];
fileList.forEach((filePath) => {
const fileName = path.basename(filePath);
const md5 = getMd5(filePath);
if (!md5Map.has(md5)) {
md5Map.set(md5, filePath);
} else {
if (fileName.match(/\(\d+\)$/)) {
deleteList.push(filePath);
} else {
deleteList.push(md5Map.get(md5));
md5Map.set(md5, filePath);
}
}
});
deleteList.forEach((filePath) => {
console.log(`Deleting File: ${filePath}`);
fs.unlinkSync(filePath);
console.log(`Deleted: ${filePath}`);
});
}
const currentDir = process.cwd();
const fileList = findFiles(currentDir);
compareMd5AndDeleteDuplicates(fileList);
会优先删除(1)的文件