如何递归地读取树中的文件,如节点js中的目录结构

时间:2022-05-26 00:32:59

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.

我有一个根目录在这个目录中说“A”我有很多目录说“1”,“2”,“3”,“4”,“5”........以及所有这些子目录我有一个名为cucumber.json的文件。我想要做的就是读取cucumber.json文件并获得累积的结果。如何使用节点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. 如何递归地读取树中的文件,如节点js中的目录结构

在下面的屏幕截图中我的根目录是“黄瓜”,里面我有很多子目录。所有这些子目录都包含一个名为cucumber.json的文件。

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

是否有任何专用节点包可以让我的工作轻松。如果需要进一步的信息,请告诉我。

1 个解决方案

#1


2  

Hi there please try the following (javascript):

您好,请尝试以下(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();
        }
      });
    })();
  });
}

#1


2  

Hi there please try the following (javascript):

您好,请尝试以下(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();
        }
      });
    })();
  });
}