Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //requiring path and fs modules
- const path = require('path');
- const fs = require('fs');
- /**
- * Read contents of files inside a folder.
- * @param {*} folderName
- * @param {*} rootDir
- *
- * @example : readFolder("data", __dirname)
- */
- function readFolder(folderName, rootDir) {
- const dirPath = path.join(rootDir, folderName);
- //passsing directoryPath and callback function
- fs.readdir(dirPath, function (err, files) {
- //handling error
- if (err) {
- return console.log('Unable to scan directory: ' + err);
- }
- //list files
- files.forEach(function (file) {
- console.log(file);
- });
- });
- }
- module.exports = {
- readFolder: readFolder
- }
Add Comment
Please, Sign In to add comment