need to get the number of images in folder from directory just by javascript.
I searched and tried several ways, but i didn't find any javascript solution.
need to get the number of images in folder from directory just by javascript.
I searched and tried several ways, but i didn't find any javascript solution.
Using frontend JS is not possible, but using NodeJS it is.
In case you want to achieve it using NodeJS, I wrote an example of reading and retrieving the count of 'png' files in a directory. This is how I achieved it:
1-Build a NodeJS project:
mkdir myProject
cd myProject
npm init -y
2-Write the example:
touch index.js
const fs = require('fs')
const dir = '.'
const fileExtension = 'png'
fs.readdir(dir, (err, files) => {
let count = 0
files.forEach(element => {
if (getFileExtension(element) === fileExtension) {
count++
}
})
console.log(count)
})
/**
*
* @param {String} fileName
* @source https://stackoverflow.com/a/12900504
* @returns
*/
const getFileExtension = fileName => {
return fileName.slice((fileName.lastIndexOf(".") - 1 >>> 0) + 2)
}
3-Execute it:
node index.js
2 // <- output