Nodejs how to read directory and grab certain files

in my Nodejs project, i have a folder named input that contains green-1.png, green-2.png, green-3.png,red-1.png, red-2.png, red-3.png files

i want to read the input directory and grab only the all the green files.

i tried this code :

 const { promisify } = require('util')

 const readdir = promisify(fs.readdir)

 const files = await readdir('../input')

it works fine but it gets me all green and red files from input folder.

how can i get only all the green ones?


fs.readdir will get all the contents of a folder. There is no way to filter the files in the function.

The best you can do is filter the files to those that are green

const filteredFiles = files.filter((f) => f.includes('green'));