2

My directory structure is as follows

|demo
  |abc
    abc.js
    abc.html
  |xyz
    test.js
    test.html
  |images
  pqr.js

I want to read the content of every file in the demo repository and its sub repositories. Need to match a pattern in every file and on a successful match, that entry should go into an array along with the filename. Any suggestions?

I tried the following code but it won't read the files inside the sub directories

var fs=require('fs');

var dir = './demo';
fs.readdir(dir,function(err,files){
  if (err) throw err;
  files.forEach(function(file){
    if(file) {
      fs.readFile('./demo/'+file, 'utf8', function (err,data) {
        if (err) {
          return console.log(err);
        }
        console.log(data);
      });
    }
  });
});

I don't want an array of directories. I want to read the content of every file in the directory and sub directories and match a string. If the string is present, enter it into an array with the filename.

Nikita Jajodia
  • 4,220
  • 3
  • 21
  • 29
  • Possible duplicate of [Get all directories within directory nodejs](https://stackoverflow.com/questions/18112204/get-all-directories-within-directory-nodejs) – Arpit Solanki Jun 23 '17 at 10:54
  • I don't want an array of directories. I want to read the content of every file in the directory and sub directories and match a string. If the string is present, enter it into an array with the filename. – Nikita Jajodia Jun 23 '17 at 10:56
  • 1
    This answer mentioned above clearly states that you can check whether its a file or directory if it is a directory then you apply a recursive function to go into sub directories and so on. If you want to to match strings then you can put a condition checking the string with filename – Arpit Solanki Jun 23 '17 at 11:04

3 Answers3

2

I suspect you're having issues with the recursion involved. Here is a sample to get you going.

WARNING. It's written synchronously to make it easier to grok and does not check for non-files (symbolic links, sockets, etc) but should get on the right path. I've also used string interpolation to build paths ... you should use path.join

function getFiles(dir) {

    // get all 'files' in this directory
    var all = fs.readdirSync(dir);

    // process each checking directories and saving files
    return all.map(file => {
        // am I a directory?
        if (fs.statSync(`${dir}/${file}`).isDirectory()) {
            // recursively scan me for my files
            return getFiles(`${dir}/${file}`);
        }
        // WARNING! I could be something else here!!!
        return `${dir}/${file}`;     // file name (see warning)
    });
}

This returns a nest array of file names you should flatMap before reading (or use recursion thru' the result)

Wainage
  • 4,892
  • 3
  • 12
  • 22
2

This chunk of code can do this very well, take a loop:

  let data = [];

  fs.readdirSync("absolute-directory", "utf8").forEach((file) => {
    data.push(fs.readFileSync("absolute-directory" + file, "utf8"));
  });

  return data;
}

Md Abdul Halim Rafi
  • 1,810
  • 1
  • 17
  • 25
1

You cant do this search "recursively".

var fs = require('fs');
var path = require('path');

var recursiveSearch = function(directoryPath) {
    fs.readdir(directoryPath, function(err, list) {
        if (err) { return }

        list.forEach(function(file) {
            file = path.resolve(directoryPath, file);

            fs.stat(file, function (err, stat) {
                if (stat && stat.isDirectory()) {
                    recursiveSearch(file);
                } else {
                    fs.readFile(file, function (err, data) {
                        // read file contents here and 
                        console.log(file);
                        console.log(data);
                    })
                }
            })
        })
    })
};


recursiveSearch('C:\\sample_directory');
Pezhman Parsaee
  • 1,209
  • 3
  • 21
  • 35