Created
May 31, 2016 13:11
-
-
Save fr34kyn01535/b6baef4a9eb59ed2f6d61b9525bd8baa to your computer and use it in GitHub Desktop.
Node.js script to load all partitions, load all recursive children folder and files, calculating the total folders size and write them to a json file
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'), | |
path = require('path') | |
if (!fs.existsSync('./partitions')){ | |
fs.mkdirSync('./partitions'); | |
} | |
function getFilesRecursive (folder,callback) { | |
var fileContents = fs.readdirSync(folder), | |
fileTree = {}; | |
var totalSize = 0; | |
fileContents.forEach(function (fileName) { | |
var err = false; | |
try{ | |
stats = fs.lstatSync(folder + '/' + fileName); | |
}catch(e){ | |
err = true; | |
} | |
if(!err){ | |
if (stats.isDirectory()) { | |
fileTree[fileName] = { | |
type: "directory" | |
}; | |
var children = getFilesRecursive(folder + '/' + fileName,function(size){ | |
fileTree[fileName].size = size; | |
totalSize += size; | |
}); | |
fileTree[fileName].children = children; | |
} else { | |
totalSize += stats.size; | |
fileTree[fileName] = { | |
size: stats.size, | |
type: "file" | |
}; | |
} | |
} | |
}); | |
if(callback) | |
callback(totalSize); | |
return fileTree; | |
}; | |
var drivelist = require('drivelist'); | |
drivelist.list(function(error, disks) { | |
if (error) throw error; | |
for (var i in disks) { | |
var disk = disks[i]; | |
var drive = {}; | |
var path = disk.mountpoint+"//"; | |
console.log("Processing: "+disk.description+" at "+path); | |
drive.info = disk; | |
drive.content = getFilesRecursive(path); | |
drive.info.used = 0; | |
for(var j in drive.content){ | |
drive.info.used+=drive.content[j].size; | |
} | |
var fileContent = JSON.stringify(drive, null, 4); | |
var filename = disk.name.replace(/[^a-z0-9]/gi, '_').toLowerCase(); | |
fs.writeFile("./partitions/"+filename+".json", fileContent); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment