Skip to content

Instantly share code, notes, and snippets.

@rainstormza
Forked from betweenbrain/read-files.js
Created August 10, 2017 08:30
Show Gist options
  • Save rainstormza/1f6762d169593bdcab3753a4d38cae3a to your computer and use it in GitHub Desktop.
Save rainstormza/1f6762d169593bdcab3753a4d38cae3a to your computer and use it in GitHub Desktop.
Node.js read multiple files, write into one
var fs = require('fs');
var Promise = require('promise');
var promises = [];
var readline = require('readline');
var readFile = function (file) {
return new Promise(function (resolve, reject) {
var lines = [];
var rl = readline.createInterface({
input: fs.createReadStream('./logs/' + file)
});
rl.on('line', function (line) {
// Split line on comma and remove quotes
var columns = line
.replace(/"/g, '')
.split(',');
lines.push(columns);
});
rl.on('close', function () {
// Add newlines to lines
lines = lines.join("\n");
resolve(lines)
});
});
};
var writeFile = function (data) {
return new Promise(function (resolve, reject) {
fs.appendFile('output.csv', data, 'utf8', function (err) {
if (err) {
resolve('Writing file error!');
} else {
reject('Writing file succeeded!');
}
});
});
};
fs.readdir('./logs', function (err, files) {
for (var i = 0; i < files.length; i++) {
promises.push(readFile(files[i]));
if (i == (files.length - 1)) {
var results = Promise.all(promises);
results.then(writeFile)
.then(function (data) {
console.log(data)
}).catch(function (err) {
console.log(err)
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment