Here is list of files in a directory:
- a.txt
- b.txt
- c.txt
- d.txt
- e.txt
- f.txt
- g.txt
- h.txt
In each .txt
files, there are sequence sorted numbers in each line like this:
\c 1 ... some text ......
\c 2 ... some text ......
\c 3 ... some text ......
\c 4 ... some text ......
\c 5 ... some text ......
\c 6 ... some text ......
\c 7 ... some text ......
\c 8 ... some text ......
...
\c 10000
I used line-reader
because I want to parse line by line and file after file.
Here is the code I wrote:
const lineReader = require('line-reader');
var Promise = require('promise');
var fs = require('fs');
if (process.argv.length <= 2) {
console.log("Usage: " + __filename + " path/to/directory");
process.exit(-1);
}
var path = process.argv[2];
async function getFileTXT(path) {
const files = await fs.promises.readdir(path);
return files.filter(file => file.toLocaleLowerCase().endsWith('txt'));
}
Promise.all([
getFileTXT(path)
]).then(function(result) {
result[0].forEach(item => {
lineReader.eachLine(path + item, function (line) {
if(line.startsWith('\\c')){
console.log(item + " : " + line.slice(2));
}
});
});
});
What I expect to print like this:
a.txt : 1
a.txt : 2
...
a.txt : 10000
b.txt : 1
...
b.txt : 10000
...
...
h.txt : 10000
But it prints out like this:
a.txt : 1
a.txt : 2
...
b.txt : 1
...
a.txt : 50
...
...
h.txt : 1
a.txt : 200
...
...
h.txt : 10000