Programming Field

Comp - DOS/Command Prompt Reference

[Windows 2000? or later] Compares two or more files or sets of files on a byte-by-byte basis and outputs whether there are differences.

Syntax

comp[.exe] [<file-set1>] [<file-set2>] [/D] [/A] [/L] [/N=<n>] [/C] [/OFF[LINE]]

Options

<file-set1> Specifies the file set (source for comparison). You can use directory names or wildcards, and in that case, all files in the directory or files matching the wildcard in will be compared.
<file-set2> Specifies the target file for comparison. When a wildcard is specified, filenames are automatically generated based on <file-set1>, and the comparison is performed with those files (behaves differently when a wildcard is specified in <file-set1>). For example, if <file-set1> is a directory name and <file-set2> is a wildcard or directory name, the comparison searches for the corresponding file in <file-set2> for all files in <file-set1> (completing missing parts from the original filename if it is a wildcard).
/D If the comparison results in files of the same size with differences, outputs the differing content, a single byte, in decimal notation.
/A

If the comparison results in files of the same size with differences, outputs the differing content, a single byte, in ASCII characters. If both /D and /A options are present, /A takes precedence.

If neither /D nor /A is specified, the differing content is output in hexadecimal notation.

/L If the comparison results in files of the same size with differences, outputs the line numbers where differences occurred. If /L is not specified, it outputs the (zero-based) offset of the differing location in hexadecimal notation.
/N=<n> During comparison, compares only the specified number of lines, counting from the beginning (<n>). When this option is specified, detailed comparison is performed even if the file sizes do not match (without exiting due to a file size mismatch error).
/C Performs case-insensitive comparison of uppercase and lowercase alphabets.
/OFF[LINE] [Windows 10 or later] Does not skip files with the offline attribute specified.

If <file-set2> is omitted, a prompt will be displayed to input <file-set2>, and if <file-set1> is omitted, a prompt will be displayed to input <file-set1>. Additionally, if no options are specified, a prompt for option input will also be displayed.

Details

Usage of Comp

Comp compares two or more files and outputs the results to the screen (standard output). It differs from Fc in the following aspects.

  • It always compares on a byte-by-byte basis, so it does not adjust for misalignment even if one side has a string with one or more extra characters on a particular line.
  • (If /N is not specified) It aborts the comparison as soon as it detects a difference in file size.
  • It is possible to output the differing value in decimal or ASCII characters when differences are detected in byte-by-byte comparison.
  • It is possible to use an option for case-insensitive byte-by-byte comparison.
  • It aborts the comparison as soon as it detects more than 10 differences.
  • It is possible to directly specify directory names as the file set for comparison (unlike Fc, which requires the use of wildcards).
  • After the comparison is completed, a prompt will be displayed asking whether to compare another file.

If the (last) file comparison results in a match, Comp returns exit code 0; if there is a mismatch, it returns exit code 1.

Samples

Let's consider comparing the following two files (data is in ASCII characters, and newline characters are CR LF).

- data1.txt

ABC_DEF_G
HIJ

- data2.txt

AbC_DE_F
GHIJ

When comparing this with ‘comp data1.txt data2.txt’, the output will be as follows.

Comparing data1.txt and data2.txt...
Compare error at OFFSET 1
file1 = 42
file2 = 62
Compare error at OFFSET 6
file1 = 46
file2 = 5F
Compare error at OFFSET 7
file1 = 5F
file2 = 46
Compare error at OFFSET 8
file1 = 47
file2 = D
Compare error at OFFSET 9
file1 = D
file2 = A
Compare error at OFFSET A
file1 = A
file2 = 47

When comparing with ‘comp data1.txt data2.txt /L /A’, the output will be as follows.

Comparing data1.txt and data2.txt...
Compare error at LINE 1
file1 = B
file2 = b
Compare error at LINE 1
file1 = F
file2 = _
Compare error at LINE 1
file1 = _
file2 = F
Compare error at LINE 1
file1 = G
file2 =
Compare error at LINE 1
file1 =
file2 =

Compare error at LINE 2
file1 =

file2 = G