forked from nicknisi/node-talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concat.js
42 lines (39 loc) · 1.3 KB
/
concat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var fs = require("fs"),
resolve = require("path").resolve;
var fileData = [],
interval = null,
done = false,
fileDir = resolve(__dirname, 'files');
fs.readdir(fileDir, function (err, files) {
if (err) {
console.log("error finding files: " + err);
} else {
files.forEach(function (file, index) {
console.log("filename: " + file);
fs.readFile(resolve(fileDir, file), "utf8", function (err, data) {
if (err) {
console.log("error reading file: " + err);
} else {
fileData.push(data);
if (index === files.length - 1) {
done = true;
}
}
});
});
interval = setInterval(function () {
if (done) {
fs.writeFile(resolve(__dirname, 'out.txt'), fileData.join("\n"), "utf8", function (err) {
if (err) {
console.log('Error writing file: ' + err);
} else {
console.log('done');
}
clearInterval(interval);
});
} else {
console.log('not done yet');
}
}.bind(this), 10);
}
});