In node.js, can I list files with wild card matching like
fs.readdirSync('C:/tmp/*.csv')?
I did not find the information on wild card matching from the fs documention.
You can use the asterisk ( * ) and the question mark ( ? ) anywhere in a search, and you can also use them together. For example, if you want to find all the files that start with home , followed by one or two characters, and ending with any extension, enter home??. * as your search term.
An asterisk (*) – matches one or more occurrences of any character, including no character. Question mark (?) – represents or matches a single occurrence of any character.
When you have a number of files named in series (for example, chap1 to chap12) or filenames with common characters (like aegis, aeon, and aerie), you can use wildcards (also called metacharacters) to specify many files at once. These special characters are * (asterisk), ? (question mark), and [ ] (square brackets).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With