0

I have a root directory say "A" inside this directory i am having lots of directories say "1","2","3","4","5"........ and in all these subdirectories i have single file called cucumber.json. All i want to do is read the cucumber.json file and get the accumulated result. How can i achieve this using node js.

In the below screen shot my root directory is "cucumber" and inside that i have lot of sub directories. All these sub directories contains a single file named cucumber.json. enter image description here

Are there any dedicated node package which can make my work easy. Let me know if any further info is required.

2 Answers 2

3

Hi there please try the following (javascript):

// Require filesystem package for IO operations
var fs = require('fs');
// Put the path you are looking for here
var path = "d:\\nodef";

//Call the function defined below
recursiveloop(path, function(err,result){
  /* begin processing of each result */
  // For each file in the array
  for(i=0;i<result.length;i++)
  {
    //Write the name of the file
    console.log('Processing: ' + result[i]);
    //Read the file
    fs.readFile(result[i], 'utf8', function(err, data){
      //If there is an error notify to the console
      if(err) console.log('Error: ' + err);
      //Parse the json object
      var obj = JSON.parse(data);
      //Print out contents
      console.log('Name: ' + obj.name);
      console.log('Position: ' + obj.position);
    })
  }
});
// Asynchronous function to read folders and files recursively
function recursiveloop(dir, done)
{
  var results = [];
  fs.readdir(dir, function(err, list){
    if (err) return done(err);
    var i = 0;
    (function next() {
      var file = list[i++];
      if (!file) return done(null, results);
      file = dir + '/' + file;
      fs.stat(file, function(err, stat) {
        if (stat && stat.isDirectory()) {
          recursiveloop(file, function(err, res) {
            results = results.concat(res);
            next();
          });
        } else {
          results.push(file);
          next();
        }
      });
    })();
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

This could be put into a module in iteself by creating a module class and defining the recursive method like so: module.exports = { // Place recursiveloop function here } Then from the calling codefile use like so: var recursiveloopclass = require('codefile.js') recursiveloopclass.recursiveloop thereby allowing for reuse
0

There is an NPM module for this:

npm dree

Example:

const dree = require('dree');
const options = {
    depth: 5,                        // To stop after 5 directory levels
    exclude: /dir_to_exclude/,       // To exclude some pahts with a regexp
    extensions: [ 'txt', 'jpg' ]     // To include only some extensions
};

const fileCallback = function (file) {
    action(file.path);
};

let tree;

// Doing it synchronously
tree = dree.scan('./dir', options, fileCallback);

// Doing it asynchronously (returns promise)
tree = await dree.scanAsync('./dir', options, fileCallback);

// Here tree contains an object representing the whole directory tree (filtered with options)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.