get the filenames in a folder in javascript

To get the filenames in a folder in JavaScript, you can use Node.js built-in module fs. fs.readdir() method reads the contents of a specified directory and returns an array of the names of the files in the directory.

Here's an example code snippet:

index.tsx
const fs = require('fs');

const directoryPath = 'path/to/folder';

fs.readdir(directoryPath, function(err, files) {
    if (err) {
        return console.log('Unable to scan directory: ' + err);
    } 

    files.forEach(function(file) {
        console.log(file);
    });
});
278 chars
14 lines

In this code, fs.readdir() takes in the file path of the directory you want to scan, and a callback function that will be passed two parameters: an error (if any) and an array of filenames.

The forEach() method is used to iterate through the files array and log each filename to the console.

Note: This example code will only work in Node.js environment.

gistlibby LogSnag