Answers for "nodejs list all files in current directory"

6

node get all files in folder

fs.readdir('./', (err, files) => {
    files.forEach(file => {
    //   console.log(file);
})});
Posted by: Guest on March-15-2021
6

node list files in directory

//requiring path and fs modules
const path = require('path');
const fs = require('fs');
//joining path of directory 
const directoryPath = path.join(__dirname, 'Documents');
//passsing directoryPath and callback function
fs.readdir(directoryPath, function (err, files) {
    //handling error
    if (err) {
        return console.log('Unable to scan directory: ' + err);
    } 
    //listing all files using forEach
    files.forEach(function (file) {
        // Do whatever you want to do with the file
        console.log(file); 
    });
});
Posted by: Guest on June-26-2020

Code answers related to "nodejs list all files in current directory"

Code answers related to "Javascript"

Browse Popular Code Answers by Language