find通配符匹配的文件

在node.js中,我可以列出具有通配符的文件吗?

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

我没有find从fs文件通配符的信息。

这不是由Node核心覆盖的。 你可以检查出这个模块 ,你以后是什么。 npmjs.org也是查找各种模块的好资源。

用法

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

如果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); }); 

不要重新发明轮子,如果你在* nix的ls工具可以很容易地做到这一点( 节点api文档 )

 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'); });