Copy

Index

The prime use of COPY is to copy one or more files to another location but it can also be used to combine (concatenate) files and to type directly to a file, printer, or other device.

Copy - Copying Files

To copy one or more files:

Syntax:

COPY source [destination] [/V] [/Y | /-Y]

sourceThe file(s) to be copied. Although this must be a single parameter, it may include multiple files specified using wildcards (* or ?). It may also be a valid device (eg. CON)
destinationThe directory and/or filename for the new file(s).
If destination...
is not specified
source is copied to the current directory with the same name and creation date as the original. If source is in the current directory, an error message is displayed stating that the "file cannot be copied to itself" .
is a valid directory name
the source file(s) are copied to the directory so named with their original names and creation dates.
is not a valid directory name but is an acceptable filename
source is copied and renamed to that filename with the original creation date. If source comprises multiple files, these are concatenated (see below) and renamed to that filename with the current date.
is unambiguously a directory name (eg. it concludes with a backslash (\)), but is not valid for some reason
an error message is displayed ("Invalid directory") and no files are copied.
a valid device (eg LPT1, COM1, etc.)
source is sent to that device.
file /AForces COPY to treat the file as an ASCII test file (see Notes below).
file /BForces COPY to treat the file as a binary file (see Notes below).
/VVerifies that new files can be read (does not compare with the original - see VERIFY).
/YNo warning prompt before overwriting a file (default when COPY is used in a batch file).
/-YDisplays a warning and requires confirmation before overwriting a file (default when COPY is used from the command line).

Notes:

  1. When used from the command line, if a file specified in destination already exists in the specified location, COPY will, by default, display a warning message and require confirmation before overwriting the old file. On the other hand, when COPY is used in a batch file, any existing files will be overwritten without warning.
    This default behaviour can be modified by presetting the /Y | /-Y switch in the COPYCMD environment variable and overruled by using the /Y | /-Y switch on the command line.

  2. COPY does not copy files that are 0 bytes long; instead, it deletes such files. Use XCOPY to copy these files.

    Source and/or Destination may be an appropriate device (such as CON, COMx or LPTx) rather than a file.

  3. Depending on context, Copy treats files as binaries or ASCII text files.
    By default:
    • When copying files from one location to another (ASCII or not), COPY assumes binary mode;
    • When concatenating files, COPY assumes ASCII mode;
    • When source or destination is a device (other than a disk), copy assumes ASCII mode.
    When operating in binary mode, COPY determines the file's starting location from the File Allocation Table and copies the number of bytes allocated to that file from that point. When in ASCII mode, data is copied until an End-Of-File ASCII #026; Ctrl-Z) character is reached. This character is NOT copied, but COPY adds an EOF character before closing the new file. This convoluted procedure enables COPY to concatenate files (see below) and to work with non-file input (the keyboard, for example). On the few occasions that the default mode is inappropriate, it may be over-ridden by adding the /A or /B switch to source and/or destination files as required.

  4. The /V switch is not really very helpful and may well not even be functional (I don't know how to test that for sure, but copying a large file with and without the /v switch took the same time as near as I could judge).

  5. The COPY command is neat, simple, (and internal) but XCOPY is far more powerful and flexible.

Tricks

Copying a file from floppy to floppy with just one drive.

The simplest and obvious way to copy a file from one floppy disk to another when there is only one drive, is to copy from one floppy to the hard drive and then MOVE the file from the hard drive to the second floppy.

Of course, the complete disk contents can be transferred using DISKCOPY A: A:.

If the hard drive is not available for any reason and only selected files are to be copied, it is possible to copy a file by entering:
COPY a:\filename b:\
and following the numerous prompts to "insert disk in drive b:" (insert destination disk in drive a:) followed by "insert disk in drive a:" (insert source disk in drive a:).

My machine (with Win98SE), is not really happy with this procedure and switches from a Dos window to full screen with the first prompt. When a copy is complete, it is easy enough to switch back to a window with [Alt+Enter] but why that should happen is beyond me.

Examples

  1. To copy "note.txt" in the current drive and directory to the directory "mynotes":
    COPY note.txt c:\mynotes
    or
    COPY note.txt c:\mynotes\
    (In the first case, if the "mynotes" directory doesn't exist, "note.txt" is copied to a file named "mynotes" in the root directory of drive C. In the second case, an "invalid Directory" error message will be displayed).
  2. To copy all the files in the "mynotes" directory to a directory named "mynotes backup" on drive D:
    COPY c:\mynotes\*.* d:\mynotes backup\
  3. To make a copy of "note.txt" in the current drive and directory and call it "program note.txt"
    COPY note.txt "program note.txt"

Copy - Concatenating Files

Syntax:

COPY source1 + source2 + ... destination [/V] [/Y | /-Y]

Notes:

  1. When concatenating files, COPY reads the first source file and writes this to destination. COPY then reads the next source file and appends this to destination, and so on.
    A ramification of this is that if destination has the same name and location as any source file (except the first) the data in that source file will be overwritten before it is read. This data would be very difficult to recover. By default, a warning message is displayed before the file is overwritten.
  2. If destination is not specified, the combined files are saved under the name of the first source file (source1). By default, a warning will be displayed asking for confirmation that source1 is to be overwritten. This can be averted by adding the /Y switch to the command.
  3. If source is specified with wildcards and destination is unambiguously a filename rather than a directory, COPY combines all files matching the filename in source and creates a single file named destination.
  4. By default, both source and destination files are assumed to be ASCII format - binary files can seldom be usefully concatenated.
  5. When files are combined, the destination file is created with the current date and time.

Examples:

  1. To combine "mar89.rpt", "apr89.rpt" and "may.rpt" into one file named "report.rpt" in the current directory:
    COPY mar89.rpt + apr89.rpt + may89.rpt report.rpt
  2. To combine all files in the current directory on the current drive that have the extension ".rpt" into one file named "combined.rpt":
    COPY *.rpt combined.rpt
  3. To combine a series of files that have ".txt" extensions with their corresponding ".ref" files to make new files with the same file names but with ".doc" extensions (ie "file1.txt" is combined with "file1.ref" to form "file1.doc", and so on).
    COPY *.txt + *.ref *.doc
  4. To combine first all files with the ".txt" extension, then all files with the ".ref" extension into one file named "combin.doc":
    COPY *.txt + *.ref combin.doc

Tricks

  1. To "touch" a file (ie. assign the current time and date without any other modification), concatenate it with nothing and save it to itself:
    COPY /b source + ,, /Y
    Note the use of the /b switch to force COPY treat the file as binary rather than an ASCII text file. The /Y switch prevents the display of the usual warning message.
    Note also that this will not work with zero-length files (which will be deleted - see above).

  2. Using the CON(sole) device as source it is possible to create a text file directly from the keyboard. Thus to create a file called (say) "output.txt", first enter:
    COPY CON output.txt
    then type the text for the file followed by a Ctrl-Z. The file is saved as "output.txt."

  3. On the same lines, text can be sent directly to the printer on LPT1. First enter:
    COPY CON LPT1
    followed by whatever text you wish to print. Type Ctrl-Z (^Z or F6) to stop the process.

  4. To add text to the beginning or end of a file, concatenate the file with con. This presents a hyphen prompt at which text can be entered from the keyboard. When done, press "Ctrl-Z" then "Enter". Thus to append text to the end of Test.txt, (and avoid the warning about overwriting Test.txt) enter:
    COPY Test.txt + CON /Y
    This will display:

    C:\WINDOWS>copy Test.txt + CON /Y
    test.txt
    CON
    _

    Now type in (say) "This is an extra line" followed by Ctrl-Z and [ENTER]. The file Test.txt will be now comprise the contents of the original Test.txt followed by This is an extra line.

  5. Text can similarly be added to the beginning of a file except that a Destination must be specified (or else the amended file is simply copied to the screen (CON)). Furthermore, Destination cannot be the same as source (Test.txt). To add text to the beginning of a file, use the form:
    COPY CON + Test.txt Test2.txt /Y

File Details:

Internal


This page last revised:
December 9, 1999.