1. Comparing Two Files with the Comp Utility
Sometimes it's helpful
to compare two files to determine what has changed or to verify that
the files are precisely the same. A common use of this technique is to
compare two text files. The files could contain anything from data to
application settings. Another way to use this technique is to verify
that no one has tampered with executable files. Viruses and other forms
of intrusion often modify executable files. Just looking at the file
isn't enough in many cases to verify the damage. However, keeping a
known good copy of the file on an unmodifiable source, such as a CD, and
using this simple comparison utility is often enough to detect the
problem. It also provides you with a way of expunging the intruder from
your executables by copying the known good executable from the CD (of
course, you still have to consider registry entries and other ways
intruders often use to rebuild themselves). This utility uses the
following syntax:
COMP [data1] [data2] [/D] [/A] [/L] [/N=number] [/C] [/OFF[LINE]]
The following list describes each of the command line arguments.
data1
Specifies the source file. You can use wildcard characters to define multiple comparisons. For example, *.DLL compares every dynamic link library in the source directory against a DLL with the same name in the destination directory.
data2
Specifies the
destination file. You can use wildcard characters to define multiple
comparisons. However, the source and destination specifications must
compare. If the source contains just one file, then the destination will
also contain one file as well.
/D
Displays the
differences in decimal format (as numeric values for each character,
whether or not there's an ASCII equivalent). This is the optimal setting
when comparing binary or executable files.
/A
Displays the
differences as ASCII characters. This is the optimal setting when
comparing text files of any type (including INI or other settings files
and standard data files in XML or other format).
/L
Displays the line
numbers for differences. The Comp utility compares the two files line
by line, where the carriage return (ASCII character 13), linefeed (ASCII
character 10), or both designate the end of a line.
/N=
numberPerforms a comparison of the specified number of lines in each file beginning with the first line.
/C
Performs a
case-insensitive comparison of the two files. Normally, Comp considers
an uppercase letter different from a lowercase letter.
/OFF[LINE]
Forces the Comp utility to compare files even when the files have the offline attribute set.
2. Copying Files with the Copy Command
The Copy
command lets you create a copy of a file that exists in one directory
into another directory. You can create copies on other drives, even
drives that you map using Windows. In addition, this command works with
nonfile devices such as the printer or keyboard. Generally, users rely
on the functionality provided by Windows Explorer to move and copy
files. However, the extensive list of command line switches provided by
the Copy command makes it
extremely flexible for use in batch commands. In addition, using some
switches, such as the ASCII file transfer option for text files, can
improve overall Windows copy performance. This command uses the
following syntax:
COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]
[+ source [/A | /B] [+ ...]] [destination [/A | /B]]
The following list describes each of the command line arguments.
source
Defines the source
of the data that you want to copy. The source is usually a file, but you
can use devices as a source of data. See the "Understanding Command
Line Devices" sidebar for details. You can use wildcard characters to
specify the filenames. See the "Working with Wildcard Characters"
sidebar for details.
destination
Defines the output
location for the data that you want to copy. The destination is usually a
file, but you can use devices as a destination for data. See the
"Understanding Command Line Devices" sidebar for details.
/A
Copies the file as ASCII text. Using this technique improves Copy command performance. However, using this command line switch with a binary file results in data loss.
/B
Copies the file
in binary mode. Many files fall into the binary category, even though
you might think they are standard text files. For example, a Word DOC
file is a binary file because it contains control and other characters
that won't transfer well using the /A command line switch.
/D
Decrypts the
destination file. This special Windows command line switch decrypts
files that are encrypted using Windows encryption features. If you don't
use this command line switch, the destination remains encrypted.
/L
Copies a symbolic
link to the target instead of the actual file pointed to by the symbolic
link when the source is a symbolic link.
/N
Creates a
destination file with an 8-character filename and a 3-character file
extension. Use this command line switch when you must create destination
files for older systems that rely on the DOS 8.3 naming convention.
Avoid using this command line switch on files with long filenames unless
you really do want to create a compatible file.
/V
Verifies the
destination file is the same as the source file. Writing files with
verification improves reliability at the cost of performance. Windows
writes the destination and then performs a file comparison when using
this command line switch.
/Y and /-Y
Suppresses or
enforces the prompt for overwriting destination files with the same name
as the destination file provided as input to the Copy command. Use the /Y command line switch in batch files where you know the batch file will overwrite an existing destination file. The /-Y command line switch is the default, so you never need to use it.
/Z
Copies files to network destinations in a restartable mode. If the network connection fails, the Copy
command gives you the option of restarting the copy from the current
file position. This command line switch makes it possible to copy large
files using less reliable network connections.
One of the more important features of the Copy
command is the ability to combine two files. For example, you might
download a large file as two file fragments from an Internet site to
reduce the problems associated with downloading a single large file.
Combine the two file fragments to create a complete file by typing a
plus sign (+) between source files like this:
COPY FilePart1 + FilePart2 CompleteFile
When the Copy command completes, CompleteFile will contain the sum of the two file fragments.
When using the Copy
command with the console, you must add an end of file marker to the
input by pressing Ctrl+Z. The end of file marker appears as ^Z on screen, which signifies it as control character 26. Pressing Enter after ^Z
copies the file to the destination you indicate. The end of file marker
doesn't appear when you send the resulting file to the console as
output. Likewise, if you want a printer to output the partial page of
data you sent to it, output a Ctrl+L character to it. You can see a
complete list of standard control characters at http://www.cs.tut.fi/~jkorpela/chars/c0.html.
You can access a
number of devices from the command line. Some devices accept input,
others output, and some accept both. These devices always reference a
physical device of some type. In some cases, the device isn't attached
to your machine, but it's accessible from your machine, such as a
network printer. Many commands and utilities let you use a device in
place of a drive letter as an argument. For example, the Copy
command lets you use input from a device to create a file. You can also
use a file as output to a device. Here's the standard list of command
line devices.
CON
The system console,
which is the combination of keyboard and monitor used to access the
computer system. Input comes from the keyboard and output goes to the
monitor.
PRN
The
default printer. You must configure network printers to provide a port
to support a command line device. The port appears on the Ports tab of
the network printer's Properties dialog box. Even if your printer can
provide bidirectional communication, the PRN device is only capable of
output.
LPT1 through LPT4
The printer
attached to the first through fourth printer (parallel) ports. The
device need not physically attach to the parallel port; Windows can
redirect the output to the physical device for you. You must configure
network printers to provide a port to provide a command line device. The
port appears on the Ports tab of the network printer's Properties
dialog box. Even if your printer can provide bidirectional
communication, the LPT devices are only capable of output.
AUX
The auxiliary
device; the one serviced by the first serial port (COM1). It's usually
better to reference COM1 directly for readability in batch files. The
AUX device is a holdover from the early days of DOS.
COM1 through COM4
The
communication device attached to the first through fourth serial ports.
Although standard outputs for this port include modems, you can connect
printers as well. The serial port can act as both an input and an output
device. You can configure network printers to use a COM port instead of
an LPT port using the Ports tab of the network printer's Properties
dialog box.
NUL
The output doesn't go anywhere. The NUL(L) device is also known as the bit bucket.
CLOCK$
This
device is supposed to access the real-time clock. In reality, the
device normally doesn't work in modern systems and Windows makes no
effort to provide required redirection. You should avoid using the
CLOCK$ device.