gpt4 book ai didi

node.js - 如何在nodejs中查找与通配符匹配的目录?

转载 作者:太空宇宙 更新时间:2023-11-04 02:00:20 24 4
gpt4 key购买 nike

在nodejs中,给定一个字符串“foo-*”,如何找到所有以foo-开头的目录名称?

例如搜索将返回 foo-bar、foo-cheese 等。

到目前为止我所拥有的:

// If The directory name contains a wildcard character
// search all directory names with the preceding characters.
let wildcard = '*';
if (directory.indexOf(wildcard) !== -1) {
// Search for directories that contain the directory string (without the *).
}

我在文件系统模块中看不到任何有用的东西。

最佳答案

在较低的级别上,有 fs.readdir()读取所有目录条目。

您可以使用它来:

  • 读取要处理的目录中的所有条目
  • 使用fs.stat()确定条目是否是目录
  • 使用类似 minimatch 的模块将目录名称与模式进行匹配
  • 如果您还想匹配嵌套目录,则可以选择递归到该目录

为了让您的生活更轻松,您可以使用glob执行上述所有操作的模块。

关于node.js - 如何在nodejs中查找与通配符匹配的目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com