Created
April 17, 2014 10:35
-
-
Save ciberado/10972390 to your computer and use it in GitHub Desktop.
NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var ejecutar = function(ruta, fnFichero, fnFinal) { | |
var numFicherosPendientes = 1; | |
var numRecursivas = 0; | |
var procesarCarpeta = function(ruta) { | |
fs.lstat(ruta, function(err, stats) { | |
if (err) { | |
numFicherosPendientes = numFicherosPendientes - 1; | |
return; | |
} | |
if (stats.isFile() === true) { | |
numFicherosPendientes = numFicherosPendientes - 1; | |
fnFichero(stats); | |
} else if (stats.isDirectory() === true) { | |
numFicherosPendientes = numFicherosPendientes - 1; | |
numRecursivas = numRecursivas + 1; | |
fs.readdir(ruta, function(err, ficheros) { | |
numRecursivas = numRecursivas -1 ; | |
if (err) return; | |
numFicherosPendientes = numFicherosPendientes + ficheros.length; | |
for (var idx=0; idx < ficheros.length; idx++) { | |
var ficheroActual = ficheros[idx]; | |
procesarCarpeta(ruta + '/' + ficheroActual); | |
} | |
}); | |
} else { | |
numFicherosPendientes = numFicherosPendientes - 1; | |
} | |
if (numFicherosPendientes === 0 && numRecursivas === 0) { | |
fnFinal(); | |
} | |
}); | |
}; | |
procesarCarpeta(ruta); | |
}; | |
var total = 0; | |
var min = 1E1000; | |
var max = 0; | |
var cuenta = 0; | |
ejecutar(__dirname, | |
function(stats) { | |
cuenta = cuenta + 1; | |
total = total + stats.size; | |
if (stats.size > max) max = stats.size; | |
if (stats.size < min) min = stats.size; | |
}, | |
function() { | |
console.log(total); | |
console.log(cuenta); | |
console.log(min); | |
console.log(max); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment