node.js 查找具有通配符匹配的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21319602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:44:16  来源:igfitidea点击:

find file with wild card matching

node.js

提问by ahala

In node.js, can I list files with wild card matching like

在 node.js 中,我可以列出通配符匹配的文件,例如

fs.readdirSync('C:/tmp/*.csv')?

I did not find the information on wild card matching from the fs documention.

我没有从fs 文档中找到有关通配符匹配的信息。

回答by Morgan ARR Allen

This is not covered by Node core. You can check out this modulefor what you are after. npmjs.org is also a great resource for finding various modules.

这不是 Node 核心所涵盖的。您可以查看此模块以了解您的需求。npmjs.org 也是查找各种模块的绝佳资源。

Usage

用法

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.
})

回答by Jaime

If you don't want to add a new dependency to your project (like glob), you can use plain js/node functions, like:

如果您不想向项目添加新的依赖项(例如glob),您可以使用普通的 js/node 函数,例如:

var files = fs.readdirSync('C:/tmp').filter(fn => fn.endsWith('.csv'));

Regexmay help in more complex comparisons

Regex可能有助于更复杂的比较

回答by Abandoned Cart

If glob is not quite what you want, or a little confusing, there is also glob-fs. The documentation covers many usage scenarios with examples.

如果 glob 不是你想要的,或者有点混乱,还有glob-fs。该文档涵盖了许多带有示例的使用场景。

// sync 
var files = glob.readdirSync('*.js', {});

// async 
glob.readdir('*.js', function(err, files) {
  console.log(files);
});

// stream 
glob.readdirStream('*.js', {})
  .on('data', function(file) {
    console.log(file);
  });

// promise 
glob.readdirPromise('*.js')
  .then(function(files) {
    console.log(file);
  });

回答by Akash Babu

Just in case you want to search files by regex (for complex matches), then consider using file-regex, which supports recursive search and concurrency control (for faster results).

万一您想通过正则表达式搜索文件(用于复杂匹配),请考虑使用file-regex,它支持递归搜索和并发控制(以获得更快的结果)。

Sample usage

示例用法

import FindFiles from 'file-regex'

// This will find all the files with extension .js
// in the given directory
const result = await FindFiles(__dirname, /\.js$/);
console.log(result)

回答by Plato

dont reinvent the wheel, if you are on *nix the lstool can easily do this (node api docs)

不要重新发明轮子,如果你在 *nix 上,这个ls工具可以很容易地做到这一点(node api docs

var options = {
  cwd: process.cwd(),
}
require('child_process')
.exec('ls -1 *.csv', options, function(err, stdout, stderr){
  if(err){ console.log(stderr); throw err };
  // remove any trailing newline, otherwise last element will be "":
  stdout = stdout.replace(/\n$/, '');
  var files = stdout.split('\n');
});