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 }); }); 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; }); } }); });