Directory Size

Post Reply
GaryT
Newbie
Posts: 8
Joined: Mon Mar 09, 2015 8:52 pm

Directory Size

Post 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
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Directory Size

Post 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.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Re: Directory Size

Post 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
Post Reply