Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of filenames in folder with Javascript

Tags:

javascript

My website is serving a lot of pictures from /assets/photos/ folder. How can I get a list of the files in that folder with Javascript?

like image 597
bevanb Avatar asked Jul 07 '15 16:07

bevanb


People also ask

How do you get a list of the names of all files present in a directory in JavaScript?

To get a list of the names of all files present in a directory in Node. js, we can call the readdir method. const testFolder = './folder/path'; const fs = require('fs'); fs. readdir(testFolder, (err, files) => { files.


2 Answers

The current code will give a list of all files in a folder, assuming it's on the server side you want to list all files:

var fs = require('fs'); var files = fs.readdirSync('/assets/photos/'); 
like image 105
Christoffer Karlsson Avatar answered Oct 01 '22 04:10

Christoffer Karlsson


No, Javascript doesn't have access to the filesystem. Server side Javascript is a whole different story but I guess you don't mean that.

like image 26
Simpal Kumar Avatar answered Oct 01 '22 05:10

Simpal Kumar