nodejs如何将多个目录文件合并成一个
作者:不吃鱼的猫咪
这篇文章主要介绍了nodejs如何将多个目录文件合并成一个问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
nodejs多个目录文件合并成一个
var fs = require('fs');
var baseUrl = './data';
var createJson = './data.json';
fs.readdir(baseUrl, function(err, files) {
if (err) {
throw err;
}
var filePath = '';
var fileReadStream;
var fileWriteStream = fs.createWriteStream(createJson);
createStramFile();
var fileNum = files.length-1;
function createStramFile(){
var currentfile = baseUrl+'/' + files.shift();
fileReadStream = fs.createReadStream(currentfile);
fileReadStream.pipe(fileWriteStream,{end:false});
fileReadStream.on("end", function() {
console.log(currentfile + ' appended');
createStramFile();
});
if (!files.length) {
fileWriteStream.end("Done");
console.log('copy Done');
return;
}
fileWriteStream.on('data',function(chunk){
console.log('copy data');
return chunk+','
});
fileWriteStream.on('close',function(){
console.log(fileNum);
if (!fileNum) {
roMockJs()
}
--fileNum;
console.log('copy over');
//roMockJs();
});
}
});nodejs将不同文件夹中的视频整合到一个文件夹中
const fs = require("fs")
const path = require("path")
main();
function main() {
const rootRealPath = path.resolve(__dirname);
const newDirName = 'newFile'; // 移动目标文件名
const newDirPath = path.resolve(__dirname + '/' + newDirName); // 新的文件路径
const filterFormatList = ['txt']; // 位移的文件后缀名
const notFilterNames = [newDirName, 'filterFile.js']; // 不进行位移的文件 或者文件夹 或 文件格式
let moveFileCount = 0; // 位移文件总数
let eachFileCount = 0; // 遍历文件总数
// 如果页面中没有 newFile 那么直接创建
if (!checkHasFile(rootRealPath, newDirName)) {
console.log(`如果没有${newDirName}文件夹 那么直接创建一个文件夹`);
fs.mkdirSync(newDirName);
}
readDirSync(rootRealPath);
console.log(`任务执行完成 操作文件总数为: ${eachFileCount} 位移文件总数为: ${moveFileCount}`);
function readDirSync(filePath) {
const pa = fs.readdirSync(filePath);
for (let i in pa) {
let ele = pa[i];
let index = i;
if (notFilterNames.indexOf(ele) !== -1) { // 如果是位移后目标文件夹 则不进行操作
break;
}
var info = fs.statSync(filePath + "/" + ele); // 判断是文件夹 还是文件
if (info.isDirectory()) {
// 递归遍历所有文件夹, 将文件夹中的文件取出
readDirSync(filePath + "/" + ele);
} else {
var splitArea = ele.split('.');
var fileName = splitArea.slice(0, -1); // 不带有格式的文件名
var fileFormat = splitArea.slice(-1); // 文件格式
eachFileCount++;
if (filterFormatList.indexOf(splitArea[splitArea.length - 1]) !== -1) {
moveFileCount++;
if (checkHasFile(newDirPath, ele)) {
fs.renameSync(path.resolve(`${filePath}/${ele}`), path.resolve(`${newDirPath}/${fileName}_${(new Date()).getTime()}.${fileFormat}`));
console.log('加时间戳 然后移动', ele);
} else {
console.log('没有同名文件 直接移动', ele);
fs.renameSync(path.resolve(filePath + "/" + ele), path.resolve(newDirPath + '/' + ele));
}
} else {
console.log('非 位移文件 不进行位移');
}
}
}
}
}
// 判断目标文件夹中是否有相同名称的文件
function checkHasFile(filePath, file) {
var pa = fs.readdirSync(filePath);
if (!pa || pa.length === 0) {
return false;
}
return (pa.indexOf(file) !== -1);
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
