nodejs实现递归读取文件并合并成一个
var fs = require("fs");
var path = require("path");
function readFileList(dir, filesList = []) {
const files = fs.readdirSync(dir);
// console.log(files);
files.forEach((item, index) => {
var fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
readFileList(path.join(dir, item), filesList); //递归读取文件
} else {
const reg = /.md/;
reg.test(fullPath) && filesList.push(fullPath);
}
});
return filesList;
}
var filesList = [];
readFileList("./test", filesList);
fs.writeFile("README.md", "", () => {}); //先清空
for (const item of filesList) {
let content = fs.readFileSync(item, "utf-8");
console.log(content);
fs.appendFile("README.md", content, () => {});
}
标签:文件,fs,filesList,nodejs,合并,item,path,var,const
From: https://www.cnblogs.com/sclweb/p/17637709.html