Page 1 of 1

Directory Size

Posted: Fri Aug 07, 2015 8:20 pm
by GaryT
Hi I'm trying to figure out the best way to get a directory/folder size. I using for loop to list all the directories. while running the for loop, i want to check the size of each directory. Do i need to be creating a new job for each directory in the for loop and then try pulling the job. Ive tried doing this but as i have read from earlier post, that it creating a temporary path, and its not actually setting the new job as the directory that i am creating. Does any body have any suggestions?

Thank you

Re: Directory Size

Posted: Fri Aug 07, 2015 9:06 pm
by gabrielp
Could you post the script you're working on? This should be pretty simple with Process.execute. If you're on a unix based OS, you should be able to use something like 'du' to get a directory size in your loop.

Re: Directory Size

Posted: Mon Aug 10, 2015 4:08 pm
by dkelly
Here's a recursive function to calculate the size of a directory and subdirectories.

Code: Select all

function calculateDirectorySize( dirName : String )
{
	var dirSize = 0;	
	var theDir = new Dir(dirName);
	var entries = theDir.entryList("*", Dir.All|Dir.NoDotAndDotDot, Dir.DirsFirst);
	for (var i=0; i<entries.length; i++) {
		var fn = dirName+"/"+entries[i];
		if (File.isDir(fn)) {
			dirSize += calculateDirectorySize(fn);
		} else {
			var f = new File(fn);
			dirSize += f.size;
			delete f;
		}
	}
	delete theDir;
	return dirSize;	
}

var dirName = "/Users/dkelly/Downloads";
var dirSize = calculateDirectorySize(dirName)
s.log(1, "files in " + dirName + " are " + Math.floor(dirSize/1048576) + " MB");

Learn Javascript scripting & more at the Advanced Enfocus Switch concepts seminar during Graph Expo 2015 trade-show
Contact dkelly@apago.com for more information