copy - File Copying
What is the copy command?
The copy
command is a command used in the command prompt to copy and paste files.
It allows you to create a duplicate file while keeping the original file intact.
This is useful when you want to back up data or create multiple documents with the same content.
You can also use options to change the file’s date and time or set encryption.
It is also used as a way to create empty files.
On this page, we explain the basic usage of the copy
command, how to set options, and provide sample code.
How to Use the copy Command
The general syntax for the copy command is as follows:
copy [options] <source> <destination>
However, the following is a more accurate representation, but the basic usage is not a problem with the syntax above.
copy [/d] [/v] [/n] [/y | /-y] [/z] [/a | /b] <Destination> [/a | /b] [+<Additional_Destination> [/a | /b] [+ ...]] [<Destination> [/a | /b]]
We will discuss the options later, but if no options are specified, the files are copied from the source path to the destination path, in that order.
If a file with the same name already exists in the destination, a confirmation message is displayed by default.
Options
Option | Description |
---|---|
/A | Copies files with the attribute set. (Enabled by default) |
/B | Copies files in binary mode. |
/D | Copies files that have their encryption cleared if they are encrypted. |
/V | Verifies that new files are written correctly. This slows down the copy process. |
/Y | Suppresses prompting to confirm that you overwrite an existing destination file. |
/-Y | Prompts to confirm that you overwrite an existing destination file. (Enabled by default) |
/Z | Copies networked files in restartable mode. |
Example of the copy command
Basic usage
Copy sample.txt
in the user directory’s document
folder and save it as copied.txt
.
Concatenate text files
You can use the copy
command to concatenate multiple text files.
Concatenate base.txt
and union.txt
in the user directory’s document
folder and save it as extention.txt
.
The contents of base.txt
and extention.txt
are as follows. union.txt
is prepared as an empty file.
Basic information
Extension information
Execute the following command in this state:
Executing the above command will change the contents of union.txt
to the following:
Basic informationExtension information
Example of using the copy command in a batch file
When using the copy command in a batch file, you can specify the /Y
option to overwrite without displaying a confirmation message.
As in the previous example, copy sample.txt
in the user directory’s document
folder and save it as copied.txt
.
@echo off
setlocal
set FOLDER=%userprofile%\Documents\
set SRC=sample.txt
set DST=copied.txt
copy /Y %FOLDER%%SRC% %FOLDER%%DST%
endlocal
exit
Confirmation Test
Finally, let’s try a confirmation test.
Since the test is completed on the client side, no data will be sent to the server.