nodejs如何將多個目錄文件合并成一個
更新時間:2023年10月27日 09:20:16 作者:不吃魚的貓咪
這篇文章主要介紹了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 那么直接創(chuàng)建
if (!checkHasFile(rootRealPath, newDirName)) {
console.log(`如果沒有${newDirName}文件夾 那么直接創(chuàng)建一個文件夾`);
fs.mkdirSync(newDirName);
}
readDirSync(rootRealPath);
console.log(`任務執(zhí)行完成 操作文件總數為: ${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);
}總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
從零開始學習Node.js系列教程三:圖片上傳和顯示方法示例
這篇文章主要介紹了Node.js圖片上傳和顯示方法,結合實例形式分析了nodejs基于http傳輸圖片文件及顯示圖片的相關實現步驟與操作技巧,需要的朋友可以參考下2017-04-04
在NodeJS中啟用ECMAScript 6小結(windos以及Linux)
ECMAScript 6 是JavaScript的下一代標準,其目標,是使得JavaScript可以用來編寫復雜的應用程序、函數庫和代碼的自動生成器(code generator)。2014-07-07

