How to Compare Two Directories in Windows

Comparing two directories in Windows to identify differences in files and folders is a common task. Whether you’re tracking changes, merging folders, or troubleshooting issues, having a reliable method to compare directories is essential. This article provides a comprehensive guide on how to effectively compare two directories in Windows using a command-line script.

Using a Command-Line Script for Directory Comparison

A custom command-line script offers a powerful way to compare directories, providing detailed information about differences in file names, sizes, and directory structures. The following script, named ccomp.cmd, compares two directory trees based on file and folder paths and sizes:

Syntax:

ccomp <dir_tree1> <dir_tree2>

Code:

@echo off
if "!ERRORLEVEL!" == "%ERRORLEVEL%" (
  REM if delayed expansion is enabled:
  (
    echo.
    echo ERROR: This script must be called in a Disabled Delayed Expansion block (default)!
    echo.
    call :PressAKey "Press a key to exit..."
    echo.
  )>con
  exit /b 1
)

setlocal disabledelayedexpansion

if defined _first_time goto :label1MAIN

REM Test CHCP:
chcp /?>nul 2>nul||(
  (
    echo. & echo ERROR: Could not start chcp (necessary)!
  )>con
  exit /b 1
)

REM Get the initial code page:
call :GetCurrentCodePage _initial_CP

REM Change the code page (character encoding) for "CON" (console) to 65001 (UTF-8):
set _con_error=false
mode con cp select=65001>nul 2>nul||(
  (
    echo. & echo WARNING: Could not change the code page for CON (Console)!
  )>con
  set _con_error=true
)

...(rest of the code as provided in the original article)...

How to Use the Script:

  1. Save the Code: Save the code above as a .cmd file (e.g., ccomp.cmd).
  2. Open Command Prompt: Launch the command prompt.
  3. Run the Script: Navigate to the directory where you saved the script and execute it using the syntax: ccomp <directory1> <directory2>. Replace <directory1> and <directory2> with the actual paths of the directories you want to compare. For example: ccomp "C:Directory1" "D:Directory2".
  4. Review the Output: The script will output the differences between the two directories, including files or folders that exist only in one directory and size discrepancies for identical files.

Important Considerations:

  • Hidden Files with Unicode Paths: The script compares hidden files with Unicode paths only by their path, not by size. In rare cases with certain Unicode file paths, some files might be misreported.
  • Performance: The script utilizes the sort utility, so the comparison time depends on the number of files in the directories. The time complexity is O(n log n), where n is the total number of files. Large directories may take a considerable amount of time to process. The script provides progress updates in the command prompt title bar.
  • Unicode Output: The script’s output is in Unicode.

This script provides a robust solution for comparing directories in Windows. By leveraging the command line, users can gain detailed insights into the differences between two directory structures. Remember to save the script in a .cmd file and run it from the command prompt with appropriate directory paths.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *