node.js - 如何递归比较 Node 中的两个目录

标签 node.js git

我想对两个目录和子文件夹中的所有文件进行比较。两个目录的文件夹结构相同,文件可能不同。将它们称为目录 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/

相关文章:

node.js - 如何使用VueJs添加img src属性

node.js - 将两个 VSTS 存储库部署到一个 Azure Web 应用

git - 使用 git add 添加除一个目录外的所有文件

git - Git 中的 merge 是对称的吗?

git - 如何将我的 Git 存储库转换为 Mercurial 并带上它的标签

javascript - 带有反应模板的 Electron 锻造应用程序的 index.html 中的 "Uncaught ReferenceError: require is not defined"

node.js - 为什么要将 Local<Value>::New 与 String::New() 一起使用

node.js - Bluebird.js 使用请求时出现未处理的拒绝错误

git - Git中如何查看文件的权限?

Javascript:使用 setTimeout 重试的函数