Copy Files in Command Prompt
The Windows Command Prompt can be very powerful once you understand some of the commands. You can get a lot more control with the Command Prompt than you would by copying and pasting in Windows Explorer. Knowing how to make the most out of the copy commands is essential if you're remotely operating a Windows server. It's also great if you want to be more efficient with your own system.
Contents
Steps
Before You Start
- Know which command to use. There are several different ways to copy files using the Windows Command Prompt. All the commands can copy files from one place to another, but there are several cases where you may want to choose one command over the others.
- [[Copy-Files-in-Command-Prompt#COPY_sub|COPY]] - This is the basic copy function. It will allow you to quickly copy files from one location to another. You can also use this command to concatenate files.
- [[Copy-Files-in-Command-Prompt#XCOPY_sub|XCOPY]] - The xcopy command allows you to copy files and directory trees. This makes it much more suitable for copying folders. xcopy also has many modifiers which gives advanced users more control over the copying process. xcopy has been deprecated in favor of robocopy, but still works.
- [[Copy-Files-in-Command-Prompt#ROBOCOPY_sub|ROBOCOPY]] - This is the newest copy command available in the Windows Command Prompt. It requires Windows Vista or later. Windows XP users can install the Windows 2003 Server Resource Kit Tools pack to use it. robocopy is designed to mirror files and directories while maintaining permissions and other attributes. It also allows for better logging and more reliable copying of large amounts of data.
COPY
- Copy a single file. The copy command is best used when copying a single file. To use the basic copy command, you'd type copy Template:Var Template:Var. For example, to copy the file Example.txt from C:\Users\desktop to D:\backup, enter the following:
Template:CodeBox
- You need to enter the file name for the copied file as well. You can use this to change the name of the copied file if you'd like.
- To copy a file from your current working directory, just type the filename for the Template:Var part of the command. For example, If performing the command above while already in the C:\Users\desktop folder, the command would look like copy Example.txt D:\backup\Example.txt
- Copy all of the files in a folder. Use the *.* wildcard to copy all of the files from a folder at once. For example, to copy all of the files from the C:\Users\desktop\ folder to D:\backup, enter the following:
Template:CodeBox
- The copy command will not copy hidden files. Use the xcopy or robocopy command instead for hidden files.
- You can copy all of the files from your current working location by typing *.* as the source. In the above example, if already in the C:\Users\desktop\ folder, type copy *.* D:\backup.
- Copy files when the file or folder has a space in the name. If your source or target location or file has a space in the name, you'll need to surround it in quotation marks. For example, to copy all of the files from C:\Users\My Documents to D:\2015 Backup, enter the following: Template:CodeBox
- Combine (concatenate) text files. One of the "hidden" functions of the copy command is its ability to join multiple files together. This is most useful with plain text files. The content from the first and second text files in the following command will be combined, in order, into the new file:Template:CodeBox
- This command assumes that file1.txt and file2.txt are located in the current directory. You'll need to add the path to the beginning of each file name for files in other locations.
XCOPY
- Use [[Copy-Files-in-Command-Prompt#COPY_sub|COPY]] for single files. You're better off with the copy command for single files. The xcopy command does not allow you indicate whether you are setting a directory or a filename as the destination.
- Use [[Copy-Files-in-Command-Prompt#ROBOCOPY_sub|ROBOCOPY]] if you're creating backup scripts. Xcopy has been deprecated and will likely be phased out of use in the near future. Robocopy can do everything xcopy can. It is also more flexible and reliable. Creating scripts with robocopy will future-proof them.
- Copy a folder to another location. xcopy's main function is to copy folders or the contents of a folder from one location to another. For example, to copy all of the contents of the C:\tools directory to the new folder D:\backup\tools, enter the following:
Template:CodeBox
- /e tells xcopy to copy all of the subdirectories in the source location as well. This includes any empty directories.
- /i tells xcopy to assume that the destination is a folder. This will force it to create the new folder during the copy process.
- It is most useful when copying files from a CD or DVD. It will remove the Read-Only attribute automatically during the copy process.
- Use xcopy to copy hidden files. One of the big advantages of xcopy over the copy command is its ability to copy hidden and system files. Add the /h modifier to copy hidden files as well. Template:CodeBox
ROBOCOPY
- Use robocopy to easily copy folders. The robocopy command replaces the xcopy command. It can quickly copy entire folders without having to worry about defining the contents. For example, to copy all of the contents of the C:\tools directory to the new folder D:\backup\tools, enter the following:
Template:CodeBox
- The /e modifier tells robocopy to include all subdirectories. This includes empty folders.
- robocopy will automatically copy hidden and system files. It will create new directories if they don't exist at the target location.
- Mirror a directory. Mirroring a directory is great for making backups. The mirror option of robocopy will copy all of the contents from the source to the destination. It will then delete anything at the destination that doesn't exist at the source. This ensures that your backup only has the latest versions of your files. For example, to mirror C:\Users\My Documents to D:\backup\My Documents, enter the following:Template:CodeBox
- This function will preserve all permissions of the original files.
- Enable restarting. You may want to include the ability to restart the process in case the connection is severed mid-copy. Template:CodeBox
- Log the copying process. robocopy allows you to create a log file. This can help you pinpoint problems or generate an archive of what's been copied.
Template:CodeBox
- The /log+ modifier will append the existing log file instead of overwriting it. If you'd prefer to just overwrite the old log file, use /log:<filename>.txt.
Related Articles
- Use MS DOS
- Share a File or Folder in Copy in Windows
- Create and Delete Files and Directories from Windows Command Prompt