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 goto label
but it compares the actual string "file1.txt" rather than the file. I've read about the COMP command, but it doesn't seem to work if I put it in an if statement. Does anybody know how to do this? Sorry, but I rarely use batch files and have little experience in them.
Thanks in advance.
[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.
The fc (file compare) command is used to compare two files. Once fc is run and completed, it returns lines that differ between the two files. If no lines differ, you will receive a message indicating such.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With