Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node list files in directory


//requiring path and fs modules
const path = require('path');
const fs = require('fs');
//joining path of directory 
const directoryPath = path.join(__dirname, 'Documents');
//passsing directoryPath and callback function
fs.readdir(directoryPath, function (err, files) {
    //handling error
    if (err) {
        return console.log('Unable to scan directory: ' + err);
    } 
    //listing all files using forEach
    files.forEach(function (file) {
        // Do whatever you want to do with the file
        console.log(file); 
    });
});
Comment

node list folders in directory

const { readdir } = require('fs')

const getDirectories = (source, callback) =>
  readdir(source, { withFileTypes: true }, (err, files) => {
    if (err) {
      callback(err)
    } else {
      callback(
        files
          .filter(dirent => dirent.isDirectory())
          .map(dirent => dirent.name)
      )
    }
  })
Comment

PREVIOUS NEXT
Code Example
Javascript :: sass node sass loder 
Javascript :: nodejs get current directory 
Javascript :: js get alphabet as array 
Javascript :: p5.js boilerplate 
Javascript :: jquery select by name attribute 
Javascript :: how to generate 6 random alphanumerals in js 
Javascript :: jquery mouseup javascript 
Javascript :: javascript get number from input 
Javascript :: jquery validate if field exists 
Javascript :: jquery checked 
Javascript :: prepend element jquery 
Javascript :: react set title of page 
Javascript :: element without a particular class jquery 
Javascript :: node read csv 
Javascript :: websocket sample code js 
Javascript :: react native spinner 
Javascript :: usehistory 
Javascript :: javascript clear symbols 
Javascript :: how to copy text on clipboard in react 
Javascript :: max value in array javascript 
Javascript :: adonis js sort by 
Javascript :: js get all select options 
Javascript :: ready function javascript 
Javascript :: alpinejs cdn 
Javascript :: javascript keywords 
Javascript :: india pincode regex 
Javascript :: moment get time am pm 
Javascript :: get img src javascript 
Javascript :: refresh page after success ajax 
Javascript :: disable split screen react native 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =