Guest User

Untitled

a guest
Feb 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. //requiring path and fs modules
  2. const path = require('path');
  3. const fs = require('fs');
  4.  
  5. /**
  6. * Read contents of files inside a folder.
  7. * @param {*} folderName
  8. * @param {*} rootDir
  9. *
  10. * @example : readFolder("data", __dirname)
  11. */
  12. function readFolder(folderName, rootDir) {
  13.  
  14. const dirPath = path.join(rootDir, folderName);
  15. //passsing directoryPath and callback function
  16. fs.readdir(dirPath, function (err, files) {
  17. //handling error
  18. if (err) {
  19. return console.log('Unable to scan directory: ' + err);
  20. }
  21. //list files
  22. files.forEach(function (file) {
  23. console.log(file);
  24. });
  25. });
  26. }
  27.  
  28.  
  29. module.exports = {
  30. readFolder: readFolder
  31. }
Add Comment
Please, Sign In to add comment