3

I am writing a shell script using the Windows NT(ish) shell scripting language. (My computer has Windows 10.) I want to check if two text files are identical. If the files are identical, I want my script to do one thing. If the files are not identical, I want my script to do something else. The script should not pause to ask for manual entry of more data.

The "Similar Questions" list has questions about Macintosh and bash shell scripts. It also has an answer about running fc or comp from the command line, which provide a verbose output. Unfortunately, fc does not return a result to %errorlevel%, and comp asks for manual data entry instead of simply exiting.

I have a copy of Tim Hill's book on Windows NT shell scripting. Page 216 has the following line of code, where %D1% and %D2% are directories, but I do not know how to modify it to work on just a single pair of files:

for /f "tokens=*" %%I in ('fc /b %D1%\*.* %D2%\*.* ^| find /v "Comparing" ^| find /v "FC: no*"') do (set /a ERRORS+=1) & (echo %%I)
Jasper
  • 133

1 Answers1

10

You can use this code:

fc /b file1 file2 > nul
if errorlevel 1 goto files_differ
[files are the same, do something here]

:files_differ
[files are not the same, do something here]

An errorlevel of 1 is returned if the files are NOT identical.

An errorlevel of 2 means that one of the files is missing.

> nul is used to hide the output of the command

Alternatively, you can use Busybox for Windows and check if the file hashes are the same.

vbnm
  • 126