Forum Discussion

tarleaa's avatar
tarleaa
Contributor
14 years ago

Compare two folders

Hello,



is there a way in TC to compare 2 folders? I know that 2 files can be compared, but how about folders?



Thanks,

Andrei
  • Hi Andrei,



    You can use the following script to compare the contents of two folders:







    function CompareTwoFolders(fldr1, fldr2)

    {

      var fso, f1, f2, fc1, fc2;  

      fso = Sys.OleObject("Scripting.FileSystemObject");  

      f1 = fso.GetFolder(fldr1);

      fc1 = new Enumerator(f1.files);

     

      f2 = fso.GetFolder(fldr2);

      fc2 = new Enumerator(f2.files);



      if (fc1.atEnd()) {

        Log.Error("The " + fldr1 + " folder is empty!");

        return false;

      }



      if (fc2.atEnd()) {

        Log.Error("The " + fldr2 + " folder is empty!");

        return false;

      }

       

      for (; !fc1.atEnd(); fc1.moveNext())

      {

          if (fc2.atEnd()) {

            Log.Error("The number of files is different!");

            return false;

          }

          filename1 = new String(fc1.item());

          filename2 = new String(fc2.item());

          filename1 = filename1.substr(filename1.lastIndexOf("\\") + 1);

          filename2 = filename2.substr(filename2.lastIndexOf("\\") + 1);

          if (filename1 != filename2)

            return false;

          if (!Files.Compare(fc1.item(), fc2.item()))

            return false;

          fc2.moveNext();        

      }

      if (!fc2.atEnd()) {

        Log.Error("The number of files is different!");

        return false;

      }  

      return true;

    }



    function Main()

    {

      if (CompareTwoFolders("C:\\temp1", "C:\\temp2")) {

        Log.Message("The contents of the folders are the same!");

      }

      else {

        Log.Error("The contents of the folders are not the same!");

      }  

    }





    I hope this helps.
  • Hi Lexi,

    thanks for the script.



    After I changed line 33

    'if (!Files.Compare(fc1.item(), fc2.item()))'

    with

    'if (! aqFile.Compare(fc1.item(), fc2.item()))'



    it worked perfect.



    Thanks again,

    Andrei
  • 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;


    }