Programming Field

Fc - DOS/Command Prompt Reference

Performs a comparison of two or more files and outputs the differences between them.

Syntax

fc[.exe] [/L] [/A] [/C] [/LB<n>] [/N] [/T] [/W] [/<nnnn>]
  [/OFF[LINE]] [/U] <file-name1> <file-name2>
fc[.exe] /B <file-name1> <file-name2>

Options

/L Compares files as text files. If neither /L nor /B is specified, the decision is based on the file extensions.
/A Outputs only the beginning and end of differing sections without displaying all differing lines. Gaps are represented with ‘...’ for brevity.
/C Does not differentiate between uppercase and lowercase letters in the alphabet.
/LB<n> Specifies the maximum number of differing lines. If the number of differing lines exceeds this limit, the comparison process is aborted. [MS-DOS] If not specified, the default value 100 is used.
/N Displays line numbers when outputting differing sections.
/T Compares tab characters (\x8) in text files as tab characters. If /T is not specified, tab characters are compared as eight spaces.
/W Considers consecutive spaces and tab characters of two or more as a single character, and ignores leading and trailing spaces and tab characters when comparing.
/<nnnn>

Specifies the minimum consecutive number of matching lines to consider as a match (specified as a combination of ‘/’ and a numerical value, such as ‘/3’). If a line matches but the lines before and after it do not, the matching line is considered non-matching if the number of matching lines falls below the specified count.

If this option is not specified, it is considered as if ‘/2’ were specified.

/OFF[LINE] [Windows NT series] Does not skip files with the offline attribute specified.
/U [Windows NT series] Compares files as Unicode text files (UCS-4).
<file-name1> Specifies the file (source of comparison) to compare. Wildcards can be used; in this case, all files matching the wildcard and <file-name2> are compared.
<file-name2> Specifies the destination file for comparison. If a wildcard is specified, files are automatically generated based on <file-name1>, and the comparison is performed with those files (behaves differently from specifying a wildcard for <file-name1>).
/B Compares <file-name1> and <file-name2> as binary files. The comparison is done on a per-byte basis, and if the file sizes differ, it is reported. Unlike text file comparison, data misalignment is not considered.

Details / Samples

Usage of Fc

Compares two or more files and outputs the results to the screen (standard output). (Similar to ‘diff’ in Linux systems, but without compatibility; Fc performs a simplified comparison.)

[Windows NT series] If the files match, Fc returns exit code 0; if they do not match, it returns 1. Additionally, using /B in Fc enables byte-by-byte comparison, while Comp always performs byte-by-byte comparison.

Compare text files

The screen output will be in the following order for <file-name1> and <file-name2>: ‘matching lines’, ‘non-matching lines (multiple)’, ‘matching lines’. Matching lines will always be output before and after the output of non-matching lines. Additionally, output is done for each set of non-matching lines.

Let's consider comparing the following two files.

- data1.txt (The space between ‘R’ and ‘S’ in the fifth line is a tab character.)

ABC
DEF
 GHIJ
KLMNOP
  QR	STUVW
XYZ12
12345
67890

- data2.txt

ABC
XXX
DEF
$$$
GHIJ
  KLM NOP
  QR  STUVW
Xyz12
12345
67890

The result of the comparison using ‘fc data1.txt data2.txt’ is output as follows.

Comparing files data1.txt and DATA2.TXT
***** data1.txt
ABC
DEF
 GHIJ
KLMNOP
  QR    STUVW
XYZ12
12345
***** DATA2.TXT
ABC
XXX
DEF
$$$
GHIJ
  KLM NOP
  QR  STUVW
Xyz12
12345
*****

The result of the comparison using ‘fc /1 data1.txt data2.txt’ is output as follows.

Comparing files data1.txt and DATA2.TXT
***** data1.txt
ABC
DEF
***** DATA2.TXT
ABC
XXX
DEF
*****

***** data1.txt
DEF
 GHIJ
KLMNOP
  QR    STUVW
XYZ12
12345
***** DATA2.TXT
DEF
$$$
GHIJ
  KLM NOP
  QR  STUVW
Xyz12
12345
*****

In contrast to the initial example, the line containing ‘DEF’ is considered a matching line.

The result of the comparison using ‘fc /C /N /W data1.txt data2.txt’ is output as follows.

Comparing files data1.txt and DATA2.TXT
***** data1.txt
    1:  ABC
    2:  DEF
    3:   GHIJ
    4:  KLMNOP
    5:    QR    STUVW
***** DATA2.TXT
    1:  ABC
    2:  XXX
    3:  DEF
    4:  $$$
    5:  GHIJ
    6:    KLM NOP
    7:    QR  STUVW
*****

The ‘/C’ flag considers ‘XYZ12’ and ‘Xyz12’ as the same, and the ‘/W’ flag treats ‘ QR STUVW’ and ‘ QR STUVW’ as identical lines. However, since ‘/1’ is not specified, the line ‘ GHIJ’ is included in the set of non-matching lines, even though the lines ‘ GHIJ’ and ‘GHIJ’ are matching because their context is not matching before and after. Additionally, with ‘/N’ specified, line numbers are included in the output.

Compare binary files

If it's a binary file, it performs a byte-by-byte comparison and displays the differing parts in hexadecimal. Additionally, it shows the offset of the differing parts.

Consider, for example, comparing the following two files (data presented in hexadecimal format).

- data1.bin

00 01 02 13 04 05 06 07

- data2.bin

00 01 02 03 04 05 16 07

If you compare this using ‘fc data1.bin data2.bin’ (files with ‘.bin’ extension are automatically compared as binary files), the output will be as follows.

Comparing files data1.bin and DATA2.BIN
00000003: 13 03
00000006: 06 16

The output represents the left values as data from <file-name1> and the right values as data from <file-name2>.

* Binary file comparison outputs all differing parts. Therefore, if the contents of the files are significantly different, a considerable amount of output will be generated.