Javascript获取文件夹中另一个折叠内的所有文件

问题描述 投票:0回答:1

如何获取文件夹内的所有js文件,而该文件夹位于另一个文件夹内?

我对JS非常陌生,我知道我可以使用readdirSync来获取它,但是我只能获取当前目录中的所有文件。

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    console.log(file + "✅"); 
    const model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

这是我的文件结构。

enter image description here

javascript node.js
1个回答
1
投票

这可以用glob解决。它是一个功能,但允许在任何级别查找文件。

var glob = require("glob")

// options is optional
glob("**/*.js", options, function (er, files) {
  // files is an array of filenames.
  // If the `nonull` option is set, and nothing
  // was found, then files is ["**/*.js"]
  // er is an error object or null.
})
© www.soinside.com 2019 - 2024. All rights reserved.