How to copy files recursively using Windows cmd?

Script examples to do a recursive Windows copy using the xcopy command.

To copy files recursively with the cmd command prompt, use the Windows xcopy command and the recursive option. The xcopy command is very similar to the copy command but it handles recursion and has many other options mainly related to recursion.

Indeed, when copying a large number of files and folders, it is useful to be able to copy all the files without naming them explicitly. This tutorial is not exhaustive, but offers a good overview of the main options, of course it is possible to combine them to have even more powerful scripts.

1. Basic xcopy option to copy files recursively

Perhaps the most important and useful option of the xcopy command is the recursive option. The “s” option indicates to copy all directories and subdirectories with their contents. An exception for empty directories. Indeed with this option, by default the system does not copy empty directories.

xcopy /s SubFolder* SubFolder_2

2. Overwrite existing files without cmd prompt

Same script, but this time using an option to avoid the prompt to overwrite existing files. Use this command with the “y” option:

xcopy /s /y SubFolder* SubFolder_2

3. Ignore recursive xcopy errors with the c option

The “c” option is used to ignore errors during copying. Useful when copying a large number of files. Thus, at the end of the copy, only the errors are to be corrected and not all the files.

xcopy /s /c SubFolder* SubFolder_2

4. Display full source and target filenames with xcopy

In this fourth command line script example, we use the recursively copy files with cmd and the xcopy f option. Use the xcopy’s f option to display full source and target filenames with paths. The paths are absolute, i.e., they start from the Windows drive letter, like C:\ for example.

Let’s check a simple example with multiple levels of subfolders. Start by displaying the structure of the folders and the files, here it is the C:\Folder. We have 8 files in 2 different folders at different levels.

dir /s /b

List source folder and subfolders before the xcopy recursive conmand
List source folder and subfolders before the xcopy recursive conmand

Now let’s copy recursively the content of the SubFolder and all other subfolders into the SubFolder_2 folder.

xcopy /s /f SubFolder SubFolder_2

Then we can double check that all the source files were copied properly in the target folder, with the same structure.

dir /s /b SubFolder_2
Copy files and folders recursively in cmd with xcopy recursive option
Copy files and folders recursively in cmd with xcopy recursive option

5. Simulate a cmd xcopy process with the ‘l’ option

The xcopy /l command is a simulation of the Copy files recursively with cmdcopy. Use the “l” option to display only a complete list of files to copy. But don’t actually copy the files. It is very useful to double check the complete file list before an actual copy.

xcopy /s /l SubFolder* SubFolder_2

6. Recursively and silently copy files with cmd and the quiet option

Tells the system not to display messages from xcopy on the prompt screen. Can be useful in batch scripts for example. The result is that the terminal only displays the total number of files copied.

xcopy /s /q SubFolder* SubFolder_2

7. Update the target folders using the xcopy u and s options

To copy only existing files on both source and destination recursively, use the xcopy /s /u option. This is a useful option to update a target folder for example. And align all latest versions in the target with a given source folder. In other words this option synchronize the content of the source and target folders.

xcopy /s /u SubFolder* SubFolder_2

8. Ensure the integrity of copied Windows files

The verification option is xcopy /v. Verifies each file after copying, verifying that the source and target are identical. Useful for sensitive data or large files.

xcopy /s /v SubFolder* SubFolder_2

9. Add a manual control step before the copy of files

Displays this message in the Windows prompt to manually start copying files: “Press any key to begin copying file(s)”. This option adds a manual control step to the copy process. Interesting in an integrated batch with other control steps.

xcopy /s /w SubFolder* SubFolder_2

In this article on xcopy, we saw some of the main options available in this powerful Windows command to copy files recursively with cmd.

MS-DOS tutorials to manage Windows files and folders