Script examples to copy Windows 10 files using the MS-DOS command prompt?
To copy files using the command line to other Windows 10 folders, use the copy script from the MS-DOS prompt. Two main options allow users to copy files in Windows 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 and recommended to script large and specific copies to avoid manipulation errors.
First, the MS-DOS scripting tool is way more efficient and effective than the drag and drop method to copy manually files and folders. Imagine you need to copy 250’000 files and need to make sure they were all copied without error. In this case, the dos copy command is the best option.
Table of Contents
1. Copy one file and keep the same name and extension
First, to copy one file from a folder to another folder and keep the same name and the same extension. It is also possible to provide only the target folder, as the name will be kept by default. If you run two times the same script, the system displays this message to allow the overwriting of the files already in the target folder:
Overwrite C:\Folder2\file.txt? (Yes/No/All):
The All option will overwrite all the files during the copy of multiple files at the same time.
copy C:\Folder\file.txt C:\Folder2\file.txt
2. Copy Windows 10 files and change the target name
In addition, you can change the name of the target file by simply specifying a different file name in the second argument. In this case the source file called file.txt is copied to another folder and is renamed as newfile.txt. To detect and copy multiple files with a specific pattern, use the wildcard symbol in the command. It allows to filter the source files by using the filename or the extension.
copy C:\Folder\file.txt C:\Folder2\newfile.txt
The copy command also allows users to list files recursively, to access folders and subfolders more effectively and have the full list of elements.
3. Copy multiple files from the command line using a filter
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 all specific files to another folder with the Windows command line, use the script below. The first option copies all the files with the txt extension and the second one copies only all the text files starting with the file prefix.
copy C:\Folder\*.txt C:\Folder2\ copy C:\Folder\file*.txt C:\Folder2\
Copying files and manage overwriting and integrity
Moreover, the dos copy command offers many other options, this list of options is not exhaustive. Like for example these useful options:
- The copy /v to check that the new files are written without errors.
- Or the copy /y to disable the prompt on overwriting.
- Then the copy /-y to activate the overwriting warning prompt.
4. Copy Windows 10 files and check their integrity using cmd
To check the integrity of the new files after the copy, use the /v copy option. The MS DOS system will then check that the copied version has no corruption issue. It is useful with sensitive data or any file you must ensure has no errors while reading. For example, a professional PowerPoint presentation. The verify option only displays a message in case of an error occurred during the byte per byte verification. If not, no specific warning or information message is displayed in the prompt.
copy /v C:\Folder\file.txt C:\Folder2\file.txt
5. Copy the files without overwrite warnings
In order not to display the prompt in case a file already exists, use the /y copy option. Then the system overwrites the target files with no explicit prompt. It means that in case of a very large copy of thousands of files, no warning will be displayed, and all the target files are overwritten.
Use this option carefully if manipulating different versions of the same file.
copy /y C:\Folder\file.txt C:\Folder2\file.txt
6. Enable a prompt before overwriting the target files
Finally, to enable the prompt explicitly during the file copy, use the same option but the opposite way. Indeed, the minus sign means disable the option: copy /-y. Use this option to avoid overwriting and potentially losing important files that has no backups. This allows to validate each file overwritten during the copy with the Yes option displayed in the prompt.
copy /-y C:\Folder\file.txt C:\Folder2\file.txt
Conclusion on copying Windows 10 files from the prompt
As a conclusion, this short tutorial shows how to use cmd scripts to copy Windows files to a different folder. And also, how to use pattern to filter filename or extension and to manage file overwriting warnings. So, it is definitely worth learning to automate copies and to avoid errors.