递归获取目录NodejS中的所有文件 [英] Get all files recursively in directories NodejS

查看:693
本文介绍了递归获取目录NodejS中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的功能有一些问题.我想在许多目录中获取所有文件.目前,我可以在传入参数的文件中检索文件.我想检索作为参数传递的文件夹中每个文件夹的html文件.我将解释是否放入参数"test",然后在"test"中检索文件,但我想检索"test/1/*.HTML","test/2/./". html:

I have a little problem with my function. I would like to get all files in many directories. Currently, I can retrieve the files in the file passed in parameters. I would like to retrieve the html files of each folder in the folder passed as a parameter. I will explain if I put in parameter "test" I retrieve the files in "test" but I would like to retrieve "test / 1 / *. Html", "test / 2 / . /.html ":

var srcpath2 = path.join('.', 'diapo', result);
function getDirectories(srcpath2) {
                return fs.readdirSync(srcpath2).filter(function (file) {
                    return fs.statSync(path.join(srcpath2, file)).isDirectory();
                });
            }

结果: [1,2,3]

The result : [1,2,3]

谢谢!

推荐答案

它看起来像 glob npm包将为您提供帮助.这是一个使用方法的示例:

It looks like the glob npm package would help you. Here is an example of how to use it:

文件层次结构:

test
├── one.html
└── test-nested
    └── two.html

JS代码:

var getDirectories = function (src, callback) {
  glob(src + '/**/*', callback);
};
getDirectories('test', function (err, res) {
  if (err) {
    console.log('Error', err);
  } else {
    console.log(res);
  }
});

其中显示:

[ 'test/one.html',
  'test/test-nested',
  'test/test-nested/two.html' ]

这篇关于递归获取目录NodejS中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆