How can I compare two files in a batch file, and perform an action based on whether or not they match? I\'ve tried something like:
if file1.txt NEQ file2.txt got
I use the below example to build reports based on file differences:
set %Batch_Work_Space_Dir%=folder for your batch file and temp resource files
set file_1=name of file
set file_2=name of file
fc %file_1% %file_1%t > %Batch_Work_Space_Dir%\Are_They_Different.txt
powershell -command "(Get-Content %Batch_Work_Space_Dir%\Are_They_Different.txt) | select -skip 1 | Set-Content %Batch_Work_Space_Dir%\Are_They_Different.txt"
set /p Diff_Found=<%Batch_Work_Space_Dir%\Are_They_Different.txt
if %Diff_Found:~0,17%" == "FC: no difference" (
execute commands
)
It seems like the COMP program is actually fairly easy to use. See this question on Yahoo answers.
Note that running comp /?
will print the help text for the program (as does specifying the /?
argument with any native Windows command line program). This outputs the same text you see in the answer of the question linked above.
Content from the Yahoo Answer:
C:\>comp /?
Compares the contents of two files or sets of files.
COMP [data1] [data2] [/D] [/A] [/L] [/N=number] [/C] [/OFF[LINE]]
data1 Specifies location and name(s) of first file(s) to compare.
data2 Specifies location and name(s) of second files to compare.
/D Displays differences in decimal format.
/A Displays differences in ASCII characters.
/L Displays line numbers for differences.
/N=number Compares only the first specified number of lines in each file.
/C Disregards case of ASCII letters when comparing files.
/OFF[LINE] Do not skip files with offline attribute set.
To compare sets of files, use wildcards in data1 and data2 parameters.
I believe you can use the "FC" command and then check the errorlevel. Here's some code:
@echo off
:main
fc c:\filename r:\filemame > nul
if errorlevel 1 goto error
:next
echo insert next CD
pause
goto main
:error
echo failed check
(Pulled from http://www.computing.net/answers/dos/batch-file-command/15753.html)