NodeJS以递归方式列出目录中的文件

时间:2022-12-01 21:45:01

I am trying to list all files in a directory (and files within any subdirectories) with the following code:

我试图使用以下代码列出目录中的所有文件(以及任何子目录中的文件):

var fs = require('fs')

var walk = function(directoryName) {
  fs.readdir(directoryName, function(e, files) {
    files.forEach(function(file) {
      fs.stat(file, function(e, f) {
        if (f.isDirectory()) {
          walk(file)
        } else {
          console.log('- ' + file)
        }
      })
    })
  })
}

walk(__dirname)

However, when my code attempts to invoke walk(file) on line 8 I get the following error:

但是,当我的代码尝试在第8行调用walk(文件)时,我收到以下错误:

TypeError: Cannot call method 'isDirectory' of undefined
    at program.js:7:15
    at Object.oncomplete (fs.js:107:15)

Why is f undefined? If I have the directory structure below, shouldn't the code identify aaa.txt and bbb.txt as files, my_dir as a directory at which point it recursively calls walk and begins the process again (with zzz.txt being the value of f)?

为什么f未定义?如果我有下面的目录结构,代码不应该将aaa.txt和bbb.txt标识为文件,my_dir作为目录,它递归地调用walk并再次开始该过程(zzz.txt是f的值) )?

- aaa.txt
- bbb.txt
+ my_dir
    - zzz.txt

2 个解决方案

#1


5  

Function fs.readdir lists the simple file names in that directory, not their absolute path. This is why the program failed to find them, thus leading to an error in fs.stat.

函数fs.readdir列出该目录中的简单文件名,而不是它们的绝对路径。这就是程序找不到它们的原因,从而导致fs.stat出错。

Here's the solution: concatenate the directory path name to the file.

这是解决方案:将目录路径名称连接到文件。

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

var walk = function(directoryName) {
  fs.readdir(directoryName, function(e, files) {
    if (e) {
      console.log('Error: ', e);
      return;
    }
    files.forEach(function(file) {
      var fullPath = path.join(directoryName,file);
      fs.stat(fullPath, function(e, f) {
        if (e) {
          console.log('Error: ', e);
          return;
        }
        if (f.isDirectory()) {
          walk(fullPath);
        } else {
          console.log('- ' + fullPath);
        }
      });
    });
  });
};

#2


0  

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

var walk = function(directoryName) {

  fs.readdir(directoryName, function(e, files) {
    files.forEach(function(file) {
      fs.stat(directoryName + path.sep + file, function(e, f) {

        if (f.isDirectory()) {
          walk(directoryName + path.sep + file)
        } else {
          console.log(' - ' + file)
        }
      })
    })
  })
}

walk(__dirname)

#1


5  

Function fs.readdir lists the simple file names in that directory, not their absolute path. This is why the program failed to find them, thus leading to an error in fs.stat.

函数fs.readdir列出该目录中的简单文件名,而不是它们的绝对路径。这就是程序找不到它们的原因,从而导致fs.stat出错。

Here's the solution: concatenate the directory path name to the file.

这是解决方案:将目录路径名称连接到文件。

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

var walk = function(directoryName) {
  fs.readdir(directoryName, function(e, files) {
    if (e) {
      console.log('Error: ', e);
      return;
    }
    files.forEach(function(file) {
      var fullPath = path.join(directoryName,file);
      fs.stat(fullPath, function(e, f) {
        if (e) {
          console.log('Error: ', e);
          return;
        }
        if (f.isDirectory()) {
          walk(fullPath);
        } else {
          console.log('- ' + fullPath);
        }
      });
    });
  });
};

#2


0  

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

var walk = function(directoryName) {

  fs.readdir(directoryName, function(e, files) {
    files.forEach(function(file) {
      fs.stat(directoryName + path.sep + file, function(e, f) {

        if (f.isDirectory()) {
          walk(directoryName + path.sep + file)
        } else {
          console.log(' - ' + file)
        }
      })
    })
  })
}

walk(__dirname)