Batch: Comparing Files

The trick to testing file contents is to do something like this:

( fc /B "%file1%" "%file2%" | find "FC: no differences encountered" ) > nul && (
  echo "%file1%" and "%file2%" are the same
) || (
  echo "%file1%" and "%file2%" are different
)

using conditional execution.

Here is an example file using this trick in a separate “function”:

@echo off

goto :main

REM *** === Function "Compare" ================================================

:compare
REM *** %1, %2 = the files
if "%~2" == "" (
  REM message to stderr
  echo error: missing argument 1>&2
) else (
  ( fc /B "%~1" "%~2" | find "FC: no differences encountered" ) > nul && (
    echo "%~1" and "%~2" are the same
  ) || (
    echo "%~1" and "%~2" are different
  )
)
goto :EOF

REM *** === Main ==============================================================

:main

call :compare path\to\some\file path\to\another\file
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: