Cross-Post: Recursive Method Problem -- Feature or Bug?

(Please forgive the cross-posting of this item. No one on the “Methods” board has responded and I’m at a loss.)

I’ve run into a problem with a method I’ve written to recursively calculate the total size of a directory’s contents. Here’s the code:

var currentDir = arguments[0];
var allDirectories = arguments[1];
var thisDir = new Array();
var dirContents = plugins.file.getFolderContents( currentDir );

var dirCount = dirContents.length;
for ( var i = 0; i < dirCount; i++)
{
   if( dirContents[i].isDirectory() )
   {
      var next = thisDir.length;
      thisDir[next] = dirContents[i];
   }
   else
   {
      globals.gDirSize += dirContents[i].size();
   }
}

for (j = 0; j < thisDir.length; j++)
{
   var next2 = allDirectories.length;
   allDirectories[next2] = thisDir[j];
   globals.gGetDirSize(thisDir[j], allDirectories);
}

return 1;

The method works as it should for folders that are not deeply nested. When things get too deep, however, I get the following error message:

Cycle detected, function gGetDirSize called 7 times (gGetDirSize, line 27)

It’s great that Servoy can catch unrestricted infinite loops, but shouldn’t there be a legitimate way to use this kind of recursive function?

Has anyone else wrestled with this and found a solution or workaround?

Thanks…

bill

I replied to this post in the Methods forum