Copying Files Using the Command Line

by Barry Dysert
(last updated July 3, 2017)

3

If you don't mind doing a little more typing and a little less mouse clicking, you can perform a lot of actions from the command line. (Displaying the Command Prompt window is covered under another tip.) One thing you can do from the command line is to copy files. The command-line copy command is COPY and it takes two (or more) parameters. The first parameter specifies the file that is to be copied, and the second parameter specifies to where the file should be copied.

So, let's say you have a file called "C:\Temp\BatFun.bat" and you want to copy it to "C:\Bat\BatFun.bat". You would type the following at the command line:

C:\> COPY C:\Temp\BatFun.bat C:\Bat\BatFun.bat

The BatFun.bat file in the \Temp directory remains there and a copy of it is created in the \Bat directory. Of course, you can specify whatever name you like for the destination, so you may have a \Temp\BatFun.bat file that you're developing and testing, but once you're satisfied with it you may want to copy it to your \Bat directory and give it the name of DirList.bat. This is accomplished by just changing the name of the destination parameter:

C:\> COPY C:\Temp\BatFun.bat C:\Bat\DirList.bat

You can use wildcard character to copy multiple files at a time. So let's say you have several .bat files in your \Temp directory that are supposed to work together, and you want to copy them all to your \Bat directory. Maybe the files are named C:\Temp\Dir1.bat, C:\Temp\Dir2.bat, and C:\Temp\Dir3.bat. To copy them all to your \Bat directory the command would be:

C:\> COPY C:\Temp\Dir?.bat C:\Bat

The wildcard character "?" stands for exactly one actual character in the source parameter. Note that you didn't need to specify anything except the destination directory (without the trailing backslash) as the second parameter. This is because COPY automatically uses the file name specified by the first parameter as the file name to be used in the \Bat directory. In other words, you'll end up with a Dir1.bat, Dir2.bat, and Dir3.bat in your C:\Bat directory.

The COPY command can also be used to concatenate (combine) files. This is done by specifying the files that are to be concatenated as the source parameter but separated by plus signs. Then you give exactly one file name as the destination parameter. All of the files separated by the plus signs will then be appended to one other, it the order specified, and that concatenated file is created where the destination parameter indicates.

So perhaps you have a few text files in your C:\Temp directory called Text1.txt, Text2.txt, and Text3.txt. You want to concatenate them into a master file (we'll also put it in your \Temp directory) called TextAll.txt. This can be done by using the following command:

C:\Temp> COPY Text1.txt+Text2.txt+Text3.txt TextAll.txt

Or you can give your fingers a break and use wildcards to achieve the same purpose:

C:\Temp> COPY Text?.txt TextAll.txt

So you don't technically have to use plus signs to separate the source files. In reality you just need to have multiple source files (whether separated by plus signs or indicated via wildcard characters) and exactly one destination file.

As with most command-line statements, there are switches that can be used with the COPY command in order to specify certain types of behavior. You can display the full syntax for the command by just invoking it and appending the "/?" switch like so:

C:\Temp> COPY /?

By looking at this output you can specify "/Y", for example, to tell COPY not to ask you about overwriting an existing file.

 This tip (13098) applies to Windows 7, 8, and 10.

Author Bio

Barry Dysert

Barry has been a computer professional for over 30 years, working in different positions such as technical team leader, project manager, and software developer.  He is currently a senior software engineer with an emphasis on developing custom applications under Microsoft Windows. ...

MORE FROM BARRY

Using Windows Defender Offline

Normally Windows Defender is used in an online environment. You can, if desired, also use it offline. This tip describes how ...

Discover More

Agent Ransack

If you need to search the contents of files fast, Agent Ransack is a tool worth having. It searches through multiple ...

Discover More

Customizing the SendTo Menu

The SendTo menu can be used to perform some handy tasks with files. By following the steps in this tip you'll be able to ...

Discover More
More WindowsTips

Displaying All the Files in a Folder using the Command Prompt

Displaying all the files a folder contains is an easy task in Windows. One way you can display the files is using command ...

Discover More

Using the Sort Command

Sorting data is a common task even of end users. Fortunately, The Windows command line provides us with a Sort utility so we ...

Discover More

Displaying the Command Prompt in Windows 8

Some commands require the use of the command prompt window. How you display that essential window in Windows 8 can be a bit ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four less than 6?

2017-07-07 04:21:35

Neil Lomas

Thanks Barry, that worked fine.


2017-07-06 06:02:00

Barry

The COPY command handles file names containing spaces just fine. Just include the file name within quotes, for example,

C:\>COPY "file with spaces.txt" "C:\New Folder\file with spaces.txt"


2017-07-06 05:49:38

Neil Lomas

For a long time, I have been unable to copy a file (using Windows Explorer), as it just crashes, but there is no problem moving a file. However, I can't move a file between disks as this automatically tries to copy instead. I can, however, move a file between my tablet or mobile phone (but not my camera!) and my main hard drive, as they are seen as media devices, not disks.

Will the COPY command handle filenames containing spaces or do I need to rename them first?


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.