Count the number of files in a directory using JavaScript/nodejs?

前端 未结 7 1585
星月不相逢
星月不相逢 2021-02-12 18:04

How can I count the number of files in a directory using nodejs with just plain JavaScript or packages? I want to do something like this:

How to count the n

相关标签:
7条回答
  • 2021-02-12 18:31

    Here the simple code,

    import RNFS from 'react-native-fs';
    RNFS.readDir(dirPath)
        .then((result) => {
         console.log(result.length);
    });
    
    0 讨论(0)
  • 2021-02-12 18:32

    Using fs, I found retrieving the directory file count to be straightforward.

    const fs = require('fs');
    const dir = './directory';
    
    fs.readdir(dir, (err, files) => {
      console.log(files.length);
    });
    
    0 讨论(0)
  • 2021-02-12 18:35
    const fs = require('fs')
    const length = fs.readdirSync('/home/directory').length
    
    0 讨论(0)
  • 2021-02-12 18:36
    const readdir = (path) => {
      return new Promise((resolve, reject) => {
        fs.readdir(path, (error, files) => {
          error ? reject(error) : resolve(files);
        });
      });
    };s
    
    readdir("---path to directory---").then((files) => {
      console.log(files.length);
    });
    
    0 讨论(0)
  • 2021-02-12 18:37

    Okay, I got a bash script like approach for this:

    const shell = require('shelljs')
    const path = require('path')
    
    module.exports.count = () => shell.exec(`cd ${path.join('path', 'to', 'folder')} || exit; ls -d -- */ | grep 'page-*' | wc -l`, { silent:true }).output
    

    That's it.

    0 讨论(0)
  • 2021-02-12 18:40

    Alternative solution without external module, maybe not the most efficient code, but will do the trick without external dependency:

    var fs = require('fs');
    
    function sortDirectory(path, files, callback, i, dir) {
        if (!i) {i = 0;}                                            //Init
        if (!dir) {dir = [];}
        if(i < files.length) {                                      //For all files
            fs.lstat(path + '\\' + files[i], function (err, stat) { //Get stats of the file
                if(err) {
                    console.log(err);
                }
                if(stat.isDirectory()) {                            //Check if directory
                    dir.push(files[i]);                             //If so, ad it to the list
                }
                sortDirectory(callback, i + 1, dir);                //Iterate
            });
        } else {
            callback(dir);                                          //Once all files have been tested, return
        }
    }
    
    function listDirectory(path, callback) {
        fs.readdir(path, function (err, files) {                    //List all files in the target directory
            if(err) {
                callback(err);                                      //Abort if error
            } else {
                sortDirectory(path, files, function (dir) {         //Get only directory
                    callback(dir);
                });
            }
        })
    }
    
    listDirectory('C:\\My\\Test\\Directory', function (dir) {
        console.log('There is ' + dir.length + ' directories: ' + dir);
    });
    
    0 讨论(0)
提交回复
热议问题