最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 15浏览 0评论

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

如何获取文件夹内的所有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;
  });

这是我的文件结构。

回答如下:

这可以用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.
})

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

如何获取文件夹内的所有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;
  });

这是我的文件结构。

回答如下:

这可以用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.
})
发布评论

评论列表 (0)

  1. 暂无评论