0

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
  • Does this answer your question? [Resolve promises one after another (i.e. in sequence)?](https://stackoverflow.com/questions/24586110/resolve-promises-one-after-another-i-e-in-sequence) – Fraser Apr 06 '20 at 13:56

1 Answers1

0

As you want to maintain the order of files, there are two approaches to handle it. If you want to read all files asynchronously, then you will have to maintain an in-memory array to store the processed result of each file at the corresponding index of the file and when processing all the files is done, then print all the content of the array in order.

But, one should be careful while storing things in-memory because memory may run out if the files are too big or there are too many of them.

Other solution is that you process all the files synchronously by running Promises synchronously using the async helper package. Or, you will have to write a synchronous version of the code.

Following is an example synchronous version:

const fs = require('fs');
const path = require('path');

const _filesDirectoryPath = path.join(__dirname, 'path/to/directory');

async function getFileTXT(path) {
    const files = await fs.promises.readdir(path);
    return files.filter(file => file.toLocaleLowerCase().endsWith('txt'));
}

function processFile(fileName) {
    const fileLocation = path.join(_filesDirectoryPath, fileName);
    const fileData = fs.readFileSync(fileLocation);
    fileData.toString().split('\n').forEach(line => {
        if(line.startsWith('\\c')){
            console.log(fileName + " : " + line.slice(2));
        }
    });
}

getFileTXT(_filesDirectoryPath)
    .then(files =>  files.forEach(fileName => processFile(fileName)));

vighnesh153
  • 4,354
  • 2
  • 13
  • 27