Skip to content

Instantly share code, notes, and snippets.

@myesn
Last active August 3, 2021 02:04
Show Gist options
  • Save myesn/d6cab2a374b2cbbb2cdedd0fa85d311e to your computer and use it in GitHub Desktop.
Save myesn/d6cab2a374b2cbbb2cdedd0fa85d311e to your computer and use it in GitHub Desktop.
比较两个文件夹中的文件, 以 origin 为基础, target 文件夹中缺失了哪些文件
const fs = require('fs');
const path = require('path');
const origin = 'C:/origin';
const target = 'C:/target';
const originFiles = fs.readdirSync(origin);
const targetFiles = fs.readdirSync(target);
originFiles.forEach((el) => {
const full = path.join(origin, el);
if (fs.lstatSync(full).isFile()) {
if (
!targetFiles.some(
(x) => filenameWithoutExtension(x) === filenameWithoutExtension(el)
)
) {
console.log(el);
}
}
});
function filenameWithoutExtension(filename) {
return filename.split('.').slice(0, -1).join('.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment