1fs.readdir('./', (err, files) => {
2 files.forEach(file => {
3 // console.log(file);
4})});
1
2//requiring path and fs modules
3const path = require('path');
4const fs = require('fs');
5//joining path of directory
6const directoryPath = path.join(__dirname, 'Documents');
7//passsing directoryPath and callback function
8fs.readdir(directoryPath, function (err, files) {
9 //handling error
10 if (err) {
11 return console.log('Unable to scan directory: ' + err);
12 }
13 //listing all files using forEach
14 files.forEach(function (file) {
15 // Do whatever you want to do with the file
16 console.log(file);
17 });
18});
1const path = require('path');
2const fs = require('fs');
3
4fs.readdir(
5 path.resolve(__dirname, 'MyFolder'),
6 (err, files) => {
7 if (err) throw err;
8
9 for (let file of files) {
10 console.log(file);
11 }
12 }
13);
1fs.readdir('./', (err, files) => {
2 files.forEach(file => {
3 // console.log(file);
4 }});