To copy a file to another folder in cmd command line, use the dos copy command in a Windows prompt. Two main options allow users to copy files in Windows command prompt or batch scripts. The options are the copy and the xcopy commands.
Of course, it is always possible to copy files and folders using the Windows system graphic user interface, but it’s more effective to script large and specific copies.
How to copy a file to another folder in cmd command line ?
Indeed, the ms-dos scripting is way more efficient and effective than drag and drop and copy manually. Imagine you need to copy 250’000 files and need to make sure they were all copied without error. The dos copy command is the best option.
First, to copy one file from a folder to another folder and keep the same name and extension:
copy C:\Folder\file.txt C:\Folder2\file.txt
In addition, you can change the name of the target name with simply changing the target file name:
copy C:\Folder\file.txt C:\Folder2\newfile.txt
Here is how to copy all files with a specific pattern, i.e. with a specific extension or a specific name before the separator.
To copy a specific file to another folder with the cmd command line, use the script below:
copy C:\Folder\*.txt C:\Folder2\ copy C:\Folder\file*.txt C:\Folder2\
Moreover the dos copy command offers many other options, like for example :
- The copy /v
- Or the copy /y
- Then the copy /-y
Note that this list of options is not exhaustive.
To check the integrity of the file or files after the copy, use the /v copy option.
copy /v C:\Folder\file.txt C:\Folder2\file.txt
In order not to display the prompt in case a file already exists. Then the system overwrites the target files with no prompt.
copy /y C:\Folder\file.txt C:\Folder2\file.txt
Finally, to enable the prompt explicitly during the file copy, use the same option but the opposite way: copy /-y
Use this option to avoid overwriting and potentially losing important files that has no back up:
copy /-y C:\Folder\file.txt C:\Folder2\file.txt
As a conclusion, this short article shows cmd examples on how to copy a file to a different location in Windows. To go one step further, here how to copy recursively files and folders with a cmd script.