Skip to content

Instantly share code, notes, and snippets.

@franciscojsc
Created February 20, 2022 18:58
Show Gist options
  • Save franciscojsc/40886539627a5c2060995c6b1c8261b8 to your computer and use it in GitHub Desktop.
Save franciscojsc/40886539627a5c2060995c6b1c8261b8 to your computer and use it in GitHub Desktop.
Find image files in a folder with Node.js
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