get list of files in a directory node code example
Example 1: how to get file name in directory node js
const fs = require('fs')
const dir = '/Users/flavio/folder'
const files = fs.readdirSync(dir)
for (const file of files) {
console.log(file)
}
Example 2: node list files in directory
const path = require('path');
const fs = require('fs');
const directoryPath = path.join(__dirname, 'Documents');
fs.readdir(directoryPath, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(function (file) {
console.log(file);
});
});
Example 3: javascript get list of files in directory
var fs = require('fs');
var files = fs.readdirSync('/downloads');
Example 4: how to get file name in directory node js
const path = require('path')
const stat = fs.lstatSync(path.join(dir, file))