find file with wild card matching
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.
// 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);
});
If you don't want to add a new dependency to your project (like glob
), you can use plain js/node functions, like:
var files = fs.readdirSync('C:/tmp').filter(fn => fn.endsWith('.csv'));
Regex
may help in more complex comparisons
This is not covered by Node core. You can check out this module for what you are after.
Setup
npm i glob
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.
})