Created
February 20, 2022 18:58
-
-
Save franciscojsc/40886539627a5c2060995c6b1c8261b8 to your computer and use it in GitHub Desktop.
Find image files in a folder with Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs/promises'); | |
const path = require('path'); | |
const user = 'francisco'; | |
const pathUserImages = `/home/${user}/Pictures/`; | |
fs.readdir(path.join('/', 'home', user, 'Pictures')) | |
.then((files) => { | |
const acceptedExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.svg']; | |
const images = files.filter((file) => | |
acceptedExtensions.includes(path.extname(file)) | |
); | |
console.log(images); | |
console.log('Count of images found', images.length); | |
}) | |
.catch((err) => { | |
console.error(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment