I would like to scan the folder, but ignore all the folders/directories that are included in it. All I have in the (C:/folder/) are .txt files and other folders, I just want to scan the txt files, and ignore the folders.
app.get('/generatE', function (req, res) {
const logsFolder = 'C:/folder/';
fs.readdir(logsFolder, (err, files) => {
if (err) {
res.send("[empty]");
return;
}
var lines = [];
files.forEach(function(filename) {
var logFileLines = fs.readFileSync (logsFolder + filename, 'ascii').toString().split("\n");
logFileLines.forEach(function(logFileLine) {
if(logFileLine.match(/.*AUDIT*./)) {
lines.push(logFileLine+'\n');
}
})
})
Use
fs.readdir
orfs.readdirSync
method with options{ withFileTypes: true }
and then do filtration usingdirent.isFile
(requires Node 10.10+).Sync version
Async version (with
async
/await
, requires Node 11+)Async version (with callbacks)