This program will compare files and report any differences. It improves on the DOS utilities, COMP.COM and the newer FC.EXE, in several respects:
License and warranty
System requirements Installation User instructions Options Environment variable Return values How spaces and tabs are handled Revision highlights |
The two versions operate the same and have the same features, except that the 32-bit version supports long filenames.
You may wish to rename the version you use more often to the
simpler CMP.EXE. All the following user instructions will assume you've
done that. Otherwise, just substitute CMP16
or
CMP32
wherever you see CMP
in the examples.
cmpThe full command form is one of
cmp [options] file1 file2 [>reportfile] cmp [options] files directory [>reportfile]In the second form, files may be any number of file specs, possibly containing wildcards, and directory may be a disk letter (with colon) or path (with or without trailing backslash). Please be aware that the 16-bit and 32-bit CMP programs expand wildcards slightly differently because the 32-bit version supports long filenames. Thus the 32-bit version would expand
abc*
to include all files, with any extension or none, whose names start
with abc; with the 16-bit version you need abc*.*
to get
the same result.
Example:
cmp -L5 zonk1 b:zonk2will compare file zonk1 (on the current drive and directory) to file zonk2 in the current directory of drive b, limiting look-ahead to five lines (the
-L5
option).
Another example:
cmp a:*.doc xx.htm b:will compare all the *.doc files in the current directory of drive a, plus xx.htm in the current directory of the current disk, to files of the same names in the current directory of drive b.
You have a lot of freedom about how you enter options. You can use
a leading hyphen or slash mark; you can use upper- or lower-case
letters. You can leave spaces between options or combine them. For
instance, the following are just some of the different ways of turning
on the W100
and B
options:
/w100 /b /w100/b /w100B -W100-B -W100 -bThis document will always use capital letters for the options, to make it easier to distinguish letter l and figure 1.
/?
Display a help message and exit with no further processing.
/0
or /1
/0
returns 0 if there are
differences or 1 if there are no differences; /1
returns
1 for differences or 0 for no differences. For more details, see
Return values below.
/B
Note that runs of spaces and/or tabs are compressed to a single space, not completely removed. Thus CMP will always consider "ab" (with no space between "a" and "b") different from "a b" (any spaces or tabs between "a" and "b").
Regardless of this option, CMP will always disregard spaces and tabs
at the ends of lines. Some more esoteric details are given below in
"How spaces and tabs are handled".
/D
/E
/E
option to make CMP keep track of blank lines and
report added or deleted blank lines as differences.
/I
/L
look-ahead
The significance of look-ahead is this. Suppose CMP finds, after lines 28-31 of file 1 match lines 38-41 of file 2, that line 32 of file 1 doesn't match line 42 of file 2. In this case, CMP has to look ahead at line 33 of file 1 and line 43 of file 2.
file 1 file 2 ================== ================== (28) line a (38) line a (29) line b (39) line b (30) line c (40) line c (31) line d (41) line d (32) line e (42) something different (33: look ahead) (43: look ahead)Maybe they match, or maybe line 43 of file 2 matches line 32 of file 1 (meaning that line 42 of file 2 is new in that file and doesn't exist in file 1). The
/L
option tells
CMP how many lines to look ahead trying to find a match after lines
that don't match. If CMP examines that number of lines from both files
without finding a match, it will report that fact and stop processing.
(If you wish, you can then re-run CMP with a higher /L
value.)
There's no specific limit for look-ahead by itself, but
/L
and /W
(below) have a combined limit.
In the 16-bit version, 64 K (65,536 bytes) is available for
look-ahead, and look-ahead times (width+2) must not
exceed that value.
In the 32-bit version, the look-ahead and width are
limited only by available memory (including virtual memory).
In either version, if you exceed the available space with the combined
/L
and /W
options, CMP will display a
message inviting you to choose lower values.
/Q
/W
, below). If any lines were
truncated, a single message will still appear at the end of
processing.
/T
The /T
option has no effect unless you also use
/B
to turn off the compression of runs of spaces.
Some more esoteric details are given below in
"How spaces and tabs are handled".
/W
width
/W
value if you suspect that some lines
contain differences beyond the original width.
You can suppress the messages about truncation of individual lines
by using the /Q
option, but CMP will still display the
message at the end so you'll know that some lines were not examined
completely and what you can do about it.
The effective width of a line, which is measured against
/W
width, may be different from that line's length
in characters, depending on how spaces and tabs
are handled (see below). If you want to know the actual maximum
effective line width in a file, simply compare the file to itself with
a small width value and the /Q option to suppress messages,
like this:
cmp /QW10 file1 file1The maximum value for
/W
depends on the value given
for /L
(above).
ORS_CMP
environment variable. You have the same freedom
as on the command line: leading slashes or hyphens, space separation
or options run together, caps or lower case.
CMP processes the environment variable before any command-line options, which means that an option on the command line will override the corresponding option in the environment variable.
The toggles, /B
/E
/I
/Q
, /T
, reverse their state every time you
specify them. So if you usually want case-blind comparisons, put
/I
in the environment variable. Then, if you want
case-sensitive comparisons for a particular run, simply put
/I
on the command line and that will reverse the setting
from the environment variable. If you have any question which options
are in effect, simply use /D
on the command line to
display all option values.
IF ERRORLEVEL
in
a batch file.
255 | bad option, or other error on the command line |
254 | specified file not available |
253 | not enough memory for combination of
/L and /W options |
2 | help message displayed (/? option, or
no files specified on the command line) |
0 | program ran to completion (whether the files are the same or different) |
You might want to use CMP in a batch file or a
makefile and take different actions depending on whether two files are
the same or different. To do this, use the /0
or
/1
option. The /1
option emulates UNIX diff
by returning an error level of 1 if the files are different or 0 if
they're the same. /0
is the opposite: it returns 0 if the
files are different or 1 if they're the same. In other words, the
/0
or /1
option gives the value CMP should
return if differences are found.
/B
and /T
options, which control the
treatment of spaces and tabs within a line.
CMP applies the /B
and /T
option settings
while reading each line from file. In fact, CMP actually makes the
changes to its own in-RAM copy of each line, so that when differences
are found CMP displays the transformed line.
CMP always ignores any spaces and tabs at the end of a line, regardless of the options. CMP also ignores any difference between the UNIX line-ending convention (LF only) and the DOS convention (CR+LF).
There can be some interaction between the /B
and
/T
option
settings and the /W
width setting. The
/W
option specifies the maximum
effective line width, but the effective line width of
a line can be less or greater than the actual length of that line in
characters:
/B
, runs of spaces and tabs are
squeezed to a single space, so the effective line width can be less
than the actual width.
/B
and not /T
, tabs are
expanded to a run of spaces, so the effective line width can be
greater than the actual line length.
/B
and /T
, tabs and
spaces are treated as ordinary characters.
/W
width, CMP will tell you the maximum effective width
at the end of the run.
Since CMP normally disregards the above differences in spacing
within a line, as well as completely blank lines, if the program finds
no differences it will report that the files are "effectively
identical". If you want to compare for character-by-character
identity, including spaces, tabs, and blank lines, specify the
/BET
options. Then if the program finds no differences it
will report that the files are identical.
/I
and /D
options. Split the
confusing three-valued /B
n option into separate
/B
and /T
toggle-type options. Change the
32-bit default to /L100
. Improve diagnostics for a bad
option in the environment variable. Convert documentation to HTML from
Word for Windows.
/0
and /1
options; systematize
all return values. No longer require the trailing backslash on a
directory argument. Instead of "effectively identical", report a more
specific phrase when the files are not significantly different based
on the /B
and /E
options.
/B
option to control that feature and tab expansion. Add
the /Q
option. Make the format of command-line options
more flexible, and scan the ORS_CMP
environment variable
for options.
/L
and /W
.
/L20
(previously /L10
).