CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Windows – COPY command – How to copy the files?

We can use COPY command to copy the files from one location to another location from the Windows Command Prompt. Through this Article we will learn how to use this command.

Copy files from one location to another location

COPY command expects source path and destination path to copy the files from source location to destination location. If you miss both these parameters you will see below message.

C:\> copy
The syntax of the command is incorrect.

You need to provide both source and destination locations to copy the file(s). For example, below command copies “sample.txt” file from one location to another location.

C:\> copy sample.txt C:\Personal\
        1 file(s) copied.

If the file exists already in the destination location; Windows will prompt to overwrite the file. Here is the example.

C:\> copy notes.txt C:\Personal\
Overwrite C:\Personal\notes.txt? (Yes/No/All):

From above, upon your confirmation, this command will overwrite the file into the destination location.

And also observe that, you can’t copy the file onto itself. If you attempt, you will see below message.

C:\> copy sample.txt sample.txt
The file cannot be copied onto itself.
        0 file(s) copied.

Append files to a single file

COPY command allows to append the files; that means, content of the files will be appended and saved into a new file. You need to mention all the files from source by adding them with plus symbol “+” and also give the destination file where to store the appended file.

Here is an example to append multiple files into a single file.

C:\Personal>copy sample.txt + notes.txt newfile.txt
sample.txt
notes.txt
        1 file(s) copied.

If you missed to pass the destination file, the files will be appended and stores the appended information into the first file which was passed through the source list. From above, if you miss to pass the destination file “newfile.txt”; the appended file will be added to “sample.txt” file only. That means, the content of the file will be replaced with the appended information. Hence you need to be cautious using this command.

Copy files from the directory

COPY command supports copying the files from one directory to another directory. You need to pass the directory details as the arguments. Note that, when you copy the directory, the command copies all the files from the source directory into the destination directory.

Here is the example, to copy the files from the directory to another directory.

C:\> copy Personal NewFolder
Personal\list.txt
Personal\numbers.txt
Personal\sample.txt
        3 file(s) copied.

Observe that, all the files from the “Personal” directory are now copied to the destination folder “NewFolder”. And also note that, only files will be copied from the directory; not the sub-directories, if any.

If you do not specify the destination folder location, all the files from the source directory will be copied to the current directory from where you are using this COPY command.

Another interesting thing we can do with this COPY command is, we can append all the files from the directory to a single file. That means, all files content will be stored into a single file. Here is the example for this.

C:\> copy NewFolder allfiles.txt
NewFolder\list.txt
NewFolder\numbers.txt
NewFolder\sample.txt
        1 file(s) copied.

Be on learning path always!

// Malin

Windows – COPY command – How to copy the files?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top