Skip to main content

How to Copy Files and Verify the Copy in Batch Script

When you copy a critical file, such as a backup, a deployment artifact, or important data, how do you know the copy is perfect? A command might report "1 file(s) copied" successfully, but network glitches, failing storage hardware, or other transient errors can introduce silent corruption, leaving the destination file different from the source. A simple copy is not enough; you need to verify.

This guide will teach you the professional standard for copying and verifying files in a batch script. You will learn to use the Robocopy command for reliable file transfers and the FC (File Compare) command for a byte-for-byte verification, ensuring that your copied data is a perfect, uncorrupted replica of the original.

The Challenge: Why Verification is Necessary

A "successful" copy operation only means that the operating system was able to write a certain number of bytes to the destination. It does not guarantee that those bytes are identical to the source. True data integrity requires a second step: a post-copy comparison to prove that the source and destination files are bit-for-bit identical.

The Best Tool for Copying: Robocopy

While COPY and XCOPY exist, Robocopy ("Robust File Copy") is the modern, professional standard for all scripted file operations in Windows. It is more reliable, especially over networks, has better logging, and provides more intelligent exit codes. For any serious script, Robocopy should be your default choice.

The Ultimate Verification Tool: FC /B

The FC (File Compare) command is the built-in Windows utility for comparing files. When used with the /B switch, it performs a binary comparison. This means it reads both files byte by byte and confirms they are exactly the same. This is the only way to be 100% certain a file is an exact copy, especially for non-text files like images, executables, or ZIP archives.

The Two-Step Process: Copy, then Verify

The most robust method for a verified copy follows this simple logic:

  1. Copy: Use Robocopy to perform the file transfer. Check its exit code to ensure the copy operation itself didn't encounter a major error.
  2. Verify: If the copy was successful, use FC /B to compare the source and destination files. Check its exit code to confirm the files are identical.

Only if both steps succeed can you be confident the operation was a success.

Basic Example: Copy and Verify a Single File

This script copies a single critical file and then performs a binary verification.

@ECHO OFF
SETLOCAL
SET "SOURCE_FILE=My-Very-Important-Backup.dat"
SET "DEST_FOLDER=E:\Backups\"

ECHO --- Verified File Copy ---
ECHO Source: %SOURCE_FILE%
ECHO Destination: %DEST_FOLDER%
ECHO.

REM Step 1: Copy the file using Robocopy.
REM The syntax 'robocopy . "destination" "file"' copies a single file.
ECHO Copying file...
robocopy . "%DEST_FOLDER%" "%SOURCE_FILE%" > NUL

REM Robocopy exit codes < 8 indicate success (files copied or nothing to do).
IF %ERRORLEVEL% GEQ 8 (
ECHO [ERROR] Robocopy failed with exit code %ERRORLEVEL%.
GOTO :End
)

ECHO Copy successful. Now verifying...

REM Step 2: Verify the copy with a binary comparison.
FC /B "%SOURCE_FILE%" "%DEST_FOLDER%\%SOURCE_FILE%" > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] Verification passed. Files are identical.
) ELSE (
ECHO [CRITICAL FAILURE] Verification failed! The copied file is corrupt or different.
)

:End
ENDLOCAL

Common Pitfalls and How to Solve Them

Understanding Robocopy's "Success" Exit Codes

This is the most confusing part of Robocopy for beginners. An ERRORLEVEL of 0 means no files were copied (they were already identical), and an ERRORLEVEL of 1 means one or more files were successfully copied. Both are success codes!

The rule of thumb for Robocopy is:

  • Exit Codes 0-7: Success. The operation completed without fatal errors.
  • Exit Codes 8 and higher: Failure. A serious error occurred (e.g., source not found, access denied).

Our script correctly checks for this with IF %ERRORLEVEL% GEQ 8.

Permissions and Administrator Rights

If you are copying files to or from protected system locations (like C:\Program Files or C:\Windows), your script may fail with an "Access Denied" error.

Solution: For these situations, the script must be run as an Administrator. Right-click your .bat file or the cmd.exe shortcut and select "Run as administrator."

Practical Example: A Robust Directory Backup Script

For backing up entire directories, running FC /B on every single file can be very slow. In this case, we rely on the robustness of Robocopy and its exit codes as the primary form of verification. Robocopy has its own internal checks, and a successful exit code is a very strong indicator of a successful copy.

This script backs up a directory and creates a detailed log file.

@ECHO OFF
SETLOCAL
REM --- Configuration ---
SET "SOURCE_DIR=C:\Users\Admin\Documents\Projects"
SET "BACKUP_DIR=E:\Backups\Projects"
SET "LOG_FILE=E:\Backups\backup_log.txt"

ECHO --- Starting Directory Backup ---
ECHO Source: %SOURCE_DIR%
ECHO Log File: %LOG_FILE%
ECHO.

REM /MIR mirrors the directory tree (copies and deletes).
REM /R:2 /W:5 sets a low retry count for locked files.
REM /LOG+ appends to the log file.
robocopy "%SOURCE_DIR%" "%BACKUP_DIR%" /MIR /R:2 /W:5 /LOG+:"%LOG_FILE%"

IF %ERRORLEVEL% LSS 8 (
ECHO [SUCCESS] Backup completed without fatal errors.
ECHO See log file for details: "%LOG_FILE%"
) ELSE (
ECHO [ERROR] Robocopy failed with a serious error.
ECHO Exit Code: %ERRORLEVEL%
ECHO Check the log file for details.
)

ENDLOCAL

Conclusion

For critical file operations, a simple copy is not enough. You must verify that the copied file is an exact replica of the source to ensure data integrity.

  • Robocopy is the best tool for the copy operation itself due to its reliability and informative exit codes.
  • FC /B is the ultimate verification tool, providing a 100% byte-for-byte comparison.
  • For single, critical files, the two-step Robocopy then FC /B process is the most secure method.
  • For entire directories, relying on a successful Robocopy exit code (< 8) is a standard and highly reliable practice that avoids the performance cost of comparing every file.