Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find file with wild card matching

Tags:

node.js

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.

like image 647
ahala Avatar asked Jan 23 '14 21:01

ahala


People also ask

How do I search for a file with a wild card character?

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.

Which wildcard character matches any symbol in a filename?

An asterisk (*) – matches one or more occurrences of any character, including no character. Question mark (?) – represents or matches a single occurrence of any character.

How do you use a wildcard in a filename?

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


1 Answers

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

like image 117
Jaime Avatar answered Nov 10 '22 01:11

Jaime