针对项目中某些特定文件夹,我们或许需要检查是否和上次有所更改,人工识别获取过于劳累,可以写个脚本来一键生成,我这里主要借用fs来实现这个功能,代码如下:
const fs = require('fs');
const path = require('path');
const folderPath = 'path/folders'; // 替换为你要检查的文件夹路径
// 定义上次检查时的文件列表文件路径
const lastCheckedFilePath = path.join(__dirname, 'last_checked_files.json');
// 读取上次检查时的文件列表
let lastCheckedFiles = [];
if (fs.existsSync(lastCheckedFilePath)) {
const lastCheckedData = fs.readFileSync(lastCheckedFilePath, 'utf8');
lastCheckedFiles = JSON.parse(lastCheckedData);
}
// 获取当前文件夹中的文件列表
const currentFiles = fs.readdirSync(folderPath);
// 查找新增的文件
const newFiles = currentFiles.filter(file => !lastCheckedFiles.includes(file));
// 打印新增文件的列表
if (newFiles.length > 0) {
console.log(`新增文件 (${newFiles.length} 个):`);
newFiles.forEach(file => console.log(file));
} else {
console.log('没有新增文件。');
}
// 保存当前文件列表到文件
const currentFilesData = JSON.stringify(currentFiles);
fs.writeFileSync(lastCheckedFilePath, currentFilesData, 'utf8');