It seems I've wandered in an old thread, but just in case someone (else) finds themselves here and wants to be able to compare folders recusively, I've cooked up the following. Note that I included an "isValid" function; you can refactor it back in, but it seemed inconvenient to test both "null" and "empty variant" over and over manually.
I've tested this against the following cases:
- One argument is invalid
- One argument is a file, not a folder
- Folders are identical with no subfolders
- Folders are identical with subfolders containing files
- Folders are different; subfolder has extra file
- Folders are different; folder content matches but file content is different
Hopefully that covers our bases...
function CompareFolders(left, right)
{
//ensure arguments are not null
if(!isValid(left) || !isValid(right)) { return false; }
//verify arguments are valid folder paths
if(!aqFileSystem.CheckAttributes(left, 16)) { return false; }
if(!aqFileSystem.CheckAttributes(right, 16)) { return false; }
//get subfolders and files
var leftSub = aqFileSystem.GetFolderInfo(left).SubFolders;
var leftFiles = aqFileSystem.GetFolderInfo(left).Files;
var rightSub = aqFileSystem.GetFolderInfo(right).SubFolders;
var rightFiles = aqFileSystem.GetFolderInfo(right).Files;
//verify file and subfolder presence
if(isValid(leftSub) != isValid(rightSub)) { return false; }
if(isValid(leftSub))
{
//verify subfolder count
if(leftSub.Count != rightSub.Count) { return false;}
for(var i = 0; i < leftSub.Count; i++)
{
//verify subfolder name and content
if(leftSub.Item(i).Name != rightSub.Item(i).Name) { return false; }
if(!CompareFolders(leftSub.Item(i).Path, rightSub.Item(i).Path)) { return false; }
}
}
if(isValid(leftFiles))
{
//verify file count
if(leftFiles.Count != rightFiles.Count) { return false; }
for(var i = 0; i < leftFiles.Count; i++)
{
var leftItem = leftFiles.Item(i);
var rightItem = rightFiles.Item(i);
//verify file name and content
if(leftItem.ShortName != rightItem.ShortName) { return false; }
if(!aqFile.Compare(leftItem.Path, rightItem.Path)) { return false; }
}
}
return true;
}
function isValid(obj)
{
if(obj == null) { return false; }
if(obj == aqObject.EmptyVariant) { return false; }
return true;
}