How to get the total volume of files in a directory?

How to get the total number of files in a directory? The best way?

+7
source share
5 answers

Here is a simple solution using the core Nodejs fs libraries in conjunction with an asynchronous library. It is completely asynchronous and should work just like the du command.

var fs = require('fs'), path = require('path'), async = require('async'); function readSizeRecursive(item, cb) { fs.lstat(item, function(err, stats) { if (!err && stats.isDirectory()) { var total = stats.size; fs.readdir(item, function(err, list) { if (err) return cb(err); async.forEach( list, function(diritem, callback) { readSizeRecursive(path.join(item, diritem), function(err, size) { total += size; callback(err); }); }, function(err) { cb(err, total); } ); }); } else { cb(err); } }); } 
+16
source

I tested the following code and it works great. Please let me know if there is something that you do not understand.

  var util = require ('util'),
 spawn = require ('child_process'). spawn,
 size = spawn ('du', ['-sh', '/ path / to / dir']);

 size.stdout.on ('data', function (data) {
   console.log ('size:' + data);
 });


 // --- Everything below is optional ---

 size.stderr.on ('data', function (data) {
   console.log ('stderr:' + data);
 });

 size.on ('exit', function (code) {
   console.log ('child process exited with code' + code);
 });

Link courtesy

Second method:

enter image description here

You can refer to the Node.js API for child_process

+3
source

Check out the functions of the node.js file system . It looks like you can use a combination of fs.readdir(path, [cb]) and fs.stat(file, [cb]) to view files in a directory and summarize their sizes.

Something like this (completely untested):

 var fs = require('fs'); fs.readdir('/path/to/dir', function(err, files) { var i, totalSizeBytes=0; if (err) throw err; for (i=0; i<files.length; i++) { fs.stat(files[i], function(err, stats) { if (err) { throw err; } if (stats.isFile()) { totalSizeBytes += stats.size; } }); } }); // Figure out how to wait for all callbacks to complete // eg by using a countdown latch, and yield total size // via a callback. 

Note that this solution only considers regular files stored directly in the target directory and does not perform recursion. A recursive solution would come naturally by checking stats.isDirectory() and input, although this probably complicates the โ€œwait for completionโ€ step.

+2
source

Use du: https://www.npmjs.org/package/du

 require('du')('/home/rvagg/.npm/', function (err, size) { console.log('The size of /home/rvagg/.npm/ is:', size, 'bytes') }) 
+2
source

ES6 Option:

 import path_module from 'path' import fs from 'fs' // computes a size of a filesystem folder (or a file) export function fs_size(path, callback) { fs.lstat(path, function(error, stats) { if (error) { return callback(error) } if (!stats.isDirectory()) { return callback(undefined, stats.size) } let total = stats.size fs.readdir(path, function(error, names) { if (error) { return callback(error) } let left = names.length if (left === 0) { return callback(undefined, total) } function done(size) { total += size left-- if (left === 0) { callback(undefined, total) } } for (let name of names) { fs_size(path_module.join(path, name), function(error, size) { if (error) { return callback(error) } done(size) }) } }) }) } 
0
source

All Articles