0

I'm trying to call the number of files in a folder into a variable so that I can set the array size into that number.

Is this possible using JS and HTML?

I've found a piece of code that might be useful. However I don't understand it well, is it useful to the function I visioned? How?

const fs = require('fs');
var dir = '../imgvids';

fs.readdir(dir, (err, files) => {
  console.log(files.length);
});

The intended porpuse is to create a variable and set the array size to it, so that then i can create a function that concatenates 'video' + 'i++' (i=0) and to stop at the size of the array so i can make a playlist of videos that autoplay muted.

  • 1
    You cannot access filesystem from HTML and JS only unless you use some backend programming languages like Node.js, PHP, Java, Python, etc – n1md7 Apr 26 '19 at 12:57
  • You could use the `readdir` function to directly create an array of all the file paths or names.(Yeah, only in Node.js) Why do you want to create an empty array of that length? – joshhemphill Apr 26 '19 at 12:58
  • [This Q/A](https://stackoverflow.com/questions/2048026/open-file-dialog-box-in-javascript) might be of help since in order to work with files in a webpage you'd have to prompt the user with a filedialog. – joshhemphill Apr 26 '19 at 13:01

1 Answers1

1

You can use fs-finder module in Node.js.

Here is a link

let Finder = require('fs-finder');

let finder = new Finder('./path/dir');

let files = finder.recursively().findFiles();

console.log(files);

This will log all the files inside directories and subdirectories

n1md7
  • 2,854
  • 1
  • 12
  • 27