Get all files recursively in directories NodejS

前端 未结 10 1258
暖寄归人
暖寄归人 2020-12-14 07:15

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 li

10条回答
  •  -上瘾入骨i
    2020-12-14 07:31

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

    File hierarchy:

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

    JS code:

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

    which displays:

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

提交回复
热议问题