Comparing Folders

On this page

GroupDocs.Comparison allows you to compare the contents of a folder (directory), process and save the result of processing. The steps for starting the comparison and configuring the display of the processing result in the resulting file are described below.

  1. Instantiate the CompareOptions object.
  2. Set the directory_compare property of CompareOptions to true.
  3. If needed, set the folder_comparison_extension to change the output format to HTML. By default, the format is TXT.
  4. If needed, set the show_only_changed to true to display only changed items.
  5. Initialize the Comparer object. Specify the path to the first compared folder and the CompareOptions object.
  6. Call the add() method of the Comparer object. Specify a path to the second folder and the CompareOptions object.
  7. Call the compare() method of the Comparer object. Specify a path to save the compare results and the CompareOptions object.

The following code snippet shows how to compare the folder1 and folder2 folders:

import groupdocs.comparison as gc

# Set output_format as 'TXT' or 'HTML'
def compare_folders(output_format, output_file_name, SOURCE_FOLDER, TARGET_FOLDER):
    # Set compare options for directory comparison
    compare_options = gc.options.CompareOptions()
    compare_options.directory_compare = True
    compare_options.folder_comparison_extension = (
        gc.options.FolderComparisonExtension.HTML
        if output_format == 'HTML'
        else gc.options.FolderComparisonExtension.TXT
    )

    # Create a new comparer with the source folder path and comparison options
    comparer = gc.Comparer(SOURCE_FOLDER, compare_options)

    # Add the target folder for comparison
    comparer.add(TARGET_FOLDER, compare_options)

    # Perform the comparison and save the result
    comparer.compare_directory(output_file_name, compare_options)

    print(f"\nFolders compared successfully.\nCheck output in {output_file_name}.")

The following images show the comparison result:

Result in HTMLResult in TXT

On this page