我想对两个目录和子文件夹中的所有文件进行比较。两个目录的文件夹结构相同,文件可能不同。将它们称为目录 A 和目录 B。
从该 id 创建目录 C 和目录 D。B 中比 A 新的文件或 A 中未找到的所有文件都应复制到 C。B 中缺少但在 A 中找到的文件应复制到目录D。
我喜欢使用 Node 和一个库,或者运行一些其他 CLI 工具,比如 git,也许它可以毫不费力地完成我所描述的操作。
有哪些好的方法可以实现这一目标?
最佳答案
以两个数组的形式获取两个目录的文件名列表,然后找出它们之间的差异。
const _ = require('lodash');
const fs = require('fs');
const aFiles = fs.readdirSync('/path/to/A');
const bFiles = fs.readdirSync('/path/to/B');
_.difference(aFiles, bFiles).forEach(v => {
// Files missing from B that are found in A should be copied to directory D
// Move file v to directory D
});
_.difference(bFiles, aFiles).forEach(v => {
// Files missing from A that are found in B should be copied to directory C
// Move file v to directory C
});
关于node.js - 如何递归比较 Node 中的两个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45340955/