Maggi Iggam
Maggi Iggam

Reputation: 692

To check if two txt files have same content using batch file

I have two text files. I want check if two files are having same content or not. For ex: File 1 has :

1 232
32 14
222 4 

File 2 has :

1 232
32 14
222 4

then output "Correct" else "Incorrect".

Upvotes: 1

Views: 3479

Answers (2)

dbenham
dbenham

Reputation: 130819

fc file1 file2 >nul&&echo Correct||echo Incorrerct

The FC command returns success (ERRORLEVEL 0) if no differences found, error (ERRORLEVEL 1) if differences found. I redirected output to NUL since you only want to know if the files are the same or not - you don't need to see a list of differences.

There are a number of options that can modify the type of comparison. Here is the documentation:

FC

Compares two files and displays the differences between them.

Syntax

fc [/a] [/b] [/c] [/l] [/lbn] [/n] [/t] [/u] [/w] [/nnnn] [drive1:][path1]filename1 [drive2:][path2]filename2

Parameters

  • /a : Abbreviates the output of an ASCII comparison. Instead of displaying all of the lines that are different, fc displays only the first and last line for each set of differences.

  • /b : Compares the files in binary mode. Fc compares the two files byte by byte and does not attempt to resynchronize the files after finding a mismatch. This is the default mode for comparing files that have the following file extensions: .exe, .com, .sys, .obj, .lib, or .bin.

  • /c : Ignores the case of letters.

  • /l : Compares the files in ASCII mode. Fc compares the two files line by line and attempts to resynchronize the files after finding a mismatch. This is the default mode for comparing files, except files with the following file extensions: .exe, .com, .sys, .obj, .lib, or .bin.

  • /lbn : Sets the n number of lines for the internal line buffer. The default length of the line buffer is 100 lines. If the files that you are comparing have more than this number of consecutive differing lines, fc cancels the comparison.

  • /n : Displays the line numbers during an ASCII comparison.

  • /t : Prevents fc from converting tabs to spaces. The default behavior is to treat tabs as spaces, with stops at each eighth character position.

  • /u : Compares files as Unicode text files.

  • /w : Compresses white space (that is, tabs and spaces) during the comparison. If a line contains many consecutive spaces or tabs, /w treats these characters as a single space. When used with the /w command-line option, fc ignores (and does not compare) white space at the beginning and end of a line.

  • /nnnn : Specifies the number of consecutive lines that must match before fc considers the files to be resynchronized. If the number of matching lines in the files is less than nnnn, fc displays the matching lines as differences. The default value is 2.

  • [drive1:][path1]filename1 : Specifies the location and name of the first file you want to compare. Filename1 is required.

  • [drive2:][path2]filename2 : Specifies the location and name of the second file you want to compare. Filename2 is required.

  • /? : Displays help at the command prompt.

Upvotes: 3

John Snow
John Snow

Reputation: 31

You can use "fc" command to do this

fc file1.txt file2.txt

It will either report differences found or inform you about file match.

Upvotes: 2

Related Questions