简体   繁体   中英

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!

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

fs.readdir(testFolder, (err, files) => {
    files.forEach(file => {
    console.log(file);
            fs.readFile(testFolder+file, 'utf8', function(err, data) {  
                if (err) throw err;
                console.log(data);
                fs.appendFile('txt_files/test.txt', data+"\n", function(err) {
                if(err) {
                    return console.log(err);
                }
                console.log("El archivo con todas las url se guardó!");
}); 

        });
    });

});

So, using what you already provided, this is what I came up with. FS has a function called appendFile which creates the file if it does not exist and then appends the data to the file's EOF. Read more on the function here

I added a check for TXT-extension so this only appends data from actual text files.

 const testFolder = './txt_files/'; const fs = require('fs'); fs.readdir(testFolder, (err, files) => { files.forEach(file => { // only match TXT Files if (file.substring(file.length - 4, file.length) == ".txt"){ fs.readfile(testFolder+file, 'utf8', (err, data) => { fs.appendFile(testFolder+'allfiles.txt', data+"\\n", function (err) { if (err) throw err; }); }); } }); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM