1const testFolder = './tests/';
2const fs = require('fs');
3
4fs.readdir(testFolder, (err, files) => {
5 files.forEach(file => {
6 console.log(file);
7 });
8});
9
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);
1const path = require('path')
2
3//...
4
5//inside the `for` loop
6const stat = fs.lstatSync(path.join(dir, file))
7