javascript - How do you get a list of the names of all files present in a directory in Node.js? -
i'm trying list of names of files present in directory using node.js. want output array of filenames. how can this?
you can use fs.readdir
or fs.readdirsync
methods.
fs.readdir
const testfolder = './tests/'; const fs = require('fs'); fs.readdir(testfolder, (err, files) => { files.foreach(file => { console.log(file); }); })
fs.readdirsync
const testfolder = './tests/'; const fs = require('fs'); fs.readdirsync(testfolder).foreach(file => { console.log(file); })
the difference between 2 methods, first 1 asynchronous, have provide callback function executed when read process ends.
the second synchronous, returns file name array, stop further execution of code until read process ends.
Comments
Post a Comment