Skip to content

Instantly share code, notes, and snippets.

@betweenbrain
Created December 5, 2016 22:35
Show Gist options
  • Save betweenbrain/12599dba164c8bc45d8dc9d70f40f5b5 to your computer and use it in GitHub Desktop.
Save betweenbrain/12599dba164c8bc45d8dc9d70f40f5b5 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)
});
}
}
});
@adam-edison
Copy link

I think you want to reject if err. The code above is resolving on error, and it's rejecting on success.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment