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:
COPY source [destination] [/V] [/Y | /-Y]
|source||The 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)|
|destination||The directory and/or filename for the new file(s). |
- 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 /A||Forces COPY to treat the file as an ASCII test file (see Notes below).|
|file /B||Forces COPY to treat the file as a binary file (see Notes below).|
|/V||Verifies that new files can be read (does not compare with the original - see VERIFY).|
|/Y||No warning prompt before overwriting a file (default when COPY is used in a batch file).|
|/-Y||Displays a warning and requires confirmation before overwriting a file (default when COPY is used from the command line).|
- 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.
- 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.
- Depending on context, Copy treats files as binaries or ASCII text files.
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.
- 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.
- The COPY command is neat, simple, (and internal) but XCOPY is far more powerful and flexible.
- To copy "note.txt" in the current drive and directory to the directory "mynotes":
COPY note.txt c:\mynotes
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).
- To copy all the files in the "mynotes" directory to a directory named "mynotes backup" on drive D:
COPY c:\mynotes\*.* d:\mynotes backup\
- 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
COPY source1 + source2 + ... destination [/V] [/Y | /-Y]
- 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.
- 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.
- 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.
- By default, both source and destination files are assumed to be ASCII format - binary files can seldom be usefully concatenated.
- When files are combined, the destination file is created with the current date and time.
- 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
- 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
- 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
- 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
- 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).
- 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."
- 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.
- 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
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.
- 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