0

I am new to nodejs and want this to work.

var fs = require('fs'); fs.readdir("dir", function(err, files) { if (err) return; files.forEach(function(f) { data = [] if f.extension = "rtf" data = data + f.data }); }); 

1 Answer 1

1

You can try this :

const fs = require('fs'); const path = require('path'); fs.readdir("dir", (err, files) => { if (err) return; files.forEach(f => { let data = [] const ext = path.extname(file) if (ext == ".rtf") { fs.readFile(f, function read(err, content) { if (err) { throw err; } data.push(content); }); } }); }); 

You will have each content of the files under the array data. But it will be better to put it into an object to know where the content come from like this :

const fs = require('fs'); const path = require('path'); fs.readdir("dir", (err, files) => { if (err) return; files.forEach(f => { let data = {} const ext = path.extname(file) if (ext == ".rtf") { fs.readFile(f, function read(err, content) { if (err) { throw err; } data[f] = content; }); } }); }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man. I am novice. I tried your code and then searched about fs module. I think I got it. Just need to add utf8 as I want to open it as text stream.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.