Monday, 22 May 2017

Read the lines of several txt files that are inside a folder and create a single text file containing all lines of text

Read the lines of several txt files that are inside a folder and create a single text file containing all lines of text. Here is an example that I make work but read only a specific file, and then i create one with the lines of text contained in that file.

const testFolder = './txt_files/';
const fs = require('fs');

fs.readdir(testFolder, (err, files) => {
        files.forEach(file => {
    console.log(file);
  });
})

fs.readFile('txt_files/url-list1.txt', 'utf8', function(err, data) {  
    if (err) throw err;
    console.log(data);

    fs.writeFile('txt_files/test.txt', data, function(err) {
            if(err) {
                return console.log(err);
            }
            console.log("El archivo con todas las url se guardó!");
        }); 
});

This example works. It reads a single file from the 'txt_files' folder, and creates a new txt with the lines of text it extracted. What I want to do is read all the files in that folder, and create a new one with all lines of text. Please help me!



via Cristian Conesa

No comments:

Post a Comment