【问题标题】:How do I use a wildcard in a to read files from a directory with Node.js如何在 a 中使用通配符从 Node.js 目录中读取文件
【发布时间】:2015-11-16 19:28:40
【问题描述】:

我需要从带有 Node.js 的目录中读取文件,文件名都是一些 .json 如何在 node.js 中的文件名部分使用通配符?

【问题讨论】:

    标签: file wildcard


    【解决方案1】:

    您可以使用glob npm 来使用通配符文件名模式匹配

    var glob = require("glob")
    
    // options is optional
    glob("DIR_PATH/*.json", options, function (er, files) {
      // files is an array of filenames.
      // er is an error object or null.
    })
    

    【讨论】:

    • 非常感谢您的回答。我真的很想弄清楚。
    • 好答案。这是异步的。
    【解决方案2】:

    当你想在单次调用中同步获取文件列表时使用glob sync

      var glob = require("glob").sync;
      logfileNames = await glob("./logs/*.log");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-29
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 2013-02-03
      • 2023-03-09
      相关资源
      最近更新 更多