copy - File Copying

Maintained on

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

OptionDescription
/ACopies files with the attribute set. (Enabled by default)
/BCopies files in binary mode.
/DCopies files that have their encryption cleared if they are encrypted.
/VVerifies that new files are written correctly. This slows down the copy process.
/YSuppresses prompting to confirm that you overwrite an existing destination file.
/-YPrompts to confirm that you overwrite an existing destination file. (Enabled by default)
/ZCopies 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.

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>copy documents/sample.txt documents/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.

base.txt
Basic information
extention.txt
Extension information

Execute the following command in this state:

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>copy documents/sample.txt documents/copied.txt
base.txt
Do you want to overwrite union.txt? (Yes/No/All): Yes
extention.txt
1 file(s) copied.
C:\users\user>

Executing the above command will change the contents of union.txt to the following:

union.txt
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.

Test1

Which of the following is a correct usage of the copy command?

Test2

If you enter “copy file1.txt file2.txt” in the command prompt, what operation will be performed?

#PowerShell #Command Prompt #batch files