Skip to main content

How to Compare Two Files for Differences in Batch Script

A common automation task is to determine if two files are identical. You might need to verify if a file copy was successful, check if a configuration has changed, or confirm that a downloaded file matches its source. Windows provides a simple, built-in command-line utility, FC (File Compare), for this exact purpose.

This guide will teach you how to use the FC command to compare two files, how to interpret its output and ERRORLEVEL to control your script's logic, and highlight the differences between its binary and text comparison modes.

The Core Command: FC (File Compare)

The FC command compares two files and reports any differences it finds. By default, it operates in a text-comparison mode, which is designed to be human-readable and find differences even if line endings or spacing vary slightly. It can also operate in a binary mode for a strict, byte-by-byte comparison.

The basic syntax is: FC [options] "file1.txt" "file2.txt"

The result of the comparison is communicated in two ways:

  • Console Output: FC prints the detected differences to the screen.
  • ERRORLEVEL: It sets a specific exit code (ERRORLEVEL) to indicate the outcome, which is perfect for scripting.

Basic Example: Comparing Two Text Files

Let's compare two simple configuration files.

config_v1.txt
[Settings]
Mode=Production
Version=1.0
config_v2.txt
[Settings]
Mode=Development
Version=1.1

And the script is:

@ECHO OFF
ECHO Comparing config_v1.txt with config_v2.txt...
FC config_v1.txt config_v2.txt

Output:

Comparing config_v1.txt with config_v2.txt...
***** config_v1.txt
Mode=Production
Version=1.0
***** config_v2.txt
Mode=Development
Version=1.1
*****
note

FC provides a clear, human-readable report of the differences.

Using ERRORLEVEL for Script Logic

While the console output is useful for humans, scripts need a more reliable way to know the result. FC sets the %ERRORLEVEL% variable based on the outcome, which we can check with an IF statement.

The possible ERRORLEVEL values for FC are:

  • 0: No differences were found (the files are identical).
  • 1: Differences were found.
  • 2: A file could not be found.

This script uses ERRORLEVEL to take action based on the comparison result.

@ECHO OFF
FC config_v1.txt config_v2.txt > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO The files are identical.
) ELSE (
ECHO The files are DIFFERENT.
)

Output:

The files are DIFFERENT.
note

We redirect the output of FC to NUL because we don't want to see the detailed difference report, only the summary from our ECHO commands.

Comparing Files in Binary Mode

For non-text files (like images, executables, or ZIP archives), a text comparison is useless. You must perform a binary comparison, which checks the files byte by byte. This is done with the /B switch.

Binary mode is also stricter and faster, making it ideal for verifying exact file copies.

Let's imagine we have two image files, logo.png and logo_copy.png. The script is the following:

@ECHO OFF
ECHO Performing a binary comparison...
FC /B logo.png logo_copy.png > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO SUCCESS: The files are an exact match.
) ELSE (
ECHO ERROR: The files are different. The copy may be corrupt.
)
note

This script will only report success if the files are 100% identical. Any difference, no matter how small, will result in an ERRORLEVEL of 1.

Common Pitfalls and How to Solve Them

Problem: Handling "File Not Found" Errors

If one of the files passed to FC does not exist, it will set an ERRORLEVEL of 2 and print an error message.

Let's see the Error:

@ECHO OFF
FC file1.txt non_existent_file.txt
ECHO ERRORLEVEL is %ERRORLEVEL%

Output:

Comparing file1.txt with non_existent_file.txt...
FC: cannot open non_existent_file.txt - No such file or directory
ERRORLEVEL is 2

Solution: Check with IF EXIST First

To provide clearer error handling, it's best to verify that both files exist before you attempt to compare them.

@ECHO OFF
SET "FILE1=file1.txt"
SET "FILE2=non_existent_file.txt"

IF NOT EXIST "%FILE1%" (
ECHO Error: The first file does not exist: %FILE1%
GOTO :EOF
)
IF NOT EXIST "%FILE2%" (
ECHO Error: The second file does not exist: %FILE2%
GOTO :EOF
)

REM Proceed with comparison only if both files exist.
FC "%FILE1%" "%FILE2%"

Problem: "FC: No Differences Encountered" Appears in Output

When files are identical, FC prints a confirmation message to the console. If you are using ERRORLEVEL for your logic, this message can be unwanted noise.

Output:

Comparing files file1.txt and FILE1.TXT
FC: no differences encountered

Solution: Redirect Output to NUL

As shown in the ERRORLEVEL examples, if you only care about the result for your script's logic, redirect the standard output of the FC command to NUL. This suppresses all informational messages but does not affect the setting of ERRORLEVEL.

REM This command runs silently.
FC /B file1.zip file2.zip > NUL

Practical Example: Verifying a File Backup

This script copies a file to a backup location and then uses FC /B to verify that the backup is an exact, uncorrupted copy of the original.

@ECHO OFF
SETLOCAL
SET "SOURCE_FILE=MyImportantData.dat"
SET "BACKUP_FOLDER=D:\Backup"
SET "BACKUP_FILE=%BACKUP_FOLDER%\%SOURCE_FILE%"

ECHO Backing up %SOURCE_FILE%...
COPY "%SOURCE_FILE%" "%BACKUP_FILE%" > NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO [ERROR] File copy failed!
GOTO :End
)

ECHO Verifying backup integrity...
FC /B "%SOURCE_FILE%" "%BACKUP_FILE%" > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] Backup verified. The files are identical.
) ELSE (
ECHO [CRITICAL ERROR] Verification failed! The backup is corrupt.
)

:End
ENDLOCAL

Conclusion

The FC command is the standard and most effective built-in tool for comparing files in Windows Batch. By understanding how to use its exit codes (%ERRORLEVEL%), you can build powerful and reliable scripts that make decisions based on whether files are the same or different.

For best results:

  • Use FC with no options for comparing human-readable text files.
  • Use FC /B for a strict, byte-by-byte binary comparison of any file type, especially for verifying copies.
  • Use an IF %ERRORLEVEL% ... check to automate logic based on the comparison result.
  • Redirect output to > NUL to suppress informational messages in your automated scripts.