Walking through the foldersNow we want to walk through all the sub-folders in the folder. Another Folder collection called SubFolders has this list, so it's time for another Enumerator:// now walk through sub-folders
var eSubFolder = new Enumerator ( oFolder.SubFolders );
while ( !eSubFolder.atEnd ( ) )
{
var oSubFolder = eSubFolder.item ( );
// recurse into sub-folder by calling myself
WalkFolders ( oSubFolder, nLevel+1, fFolderNotify, fFileNotify );
eSubFolder.moveNext ( );
}
|
In this loop we encounter another widely used method, recursion - a function calling itself until a condition is met. Used wisely, recursion is a very powerful tool, but you must bear one thing in mind. Each time a function calls another, data is "pushed" onto the stack. When the function returns, that data is "popped" from the stack, and execution continues as before. But, because the stack cannot grow past a certain size, uncontrolled recursion can easily "overflow the stack", and crash the application. Not good. Keep your local declarations few, and the levels of recursion small, and you'll be ok. It is unlikely (maybe impossible) that this code will ever overflow the stack because folders are rarely very deep. You'll see that I pass nLevel+1 into the function. This is keeping track of how deep into the sub-folders I'm getting. You may have seen earlier that this is passed into the fFolderNotify and fFileNotify functions, so they can perhaps use this information. Part 5: WalkFolders( ) in action... |