When working with file operations in Windows, Robocopy (Robust File Copy) remains the go-to tool for system administrators and developers. A common scenario involves copying files from a parent directory while intentionally excluding all its subfolders - something that isn't immediately obvious in Robocopy's syntax.
The key to solving this lies in Robocopy's directory exclusion parameter (/XD). While often used to exclude specific directories, we can leverage it to exclude all subfolders of our target directory:
robocopy C:\source\parent C:\destination\parent /XD C:\source\parent\* /S
This command copies all files from parent folder while excluding any subdirectories. The /S parameter ensures we copy non-empty directories.
For more precise control, you can modify the approach:
robocopy C:\source\parent C:\destination\parent /XD * /S
The wildcard (*) tells Robocopy to exclude all directories at the current level, effectively preventing subfolder copying while maintaining the parent directory structure.
1. The /XD parameter only excludes the specified subdirectories, not their parent
2. Empty directories won't be copied unless you use /E instead of /S
3. Hidden and system files require /COPYALL or specific copy flags
4. For network paths, consider adding /ZB for backup mode
Here's a production-ready example with logging and error handling:
robocopy \\server\share\project_files D:\backups\project_files /XD \\server\share\project_files\* /S /ZB /R:1 /W:1 /LOG:C:\logs\project_copy.log /NP /TEE
Problem: Some subfolders still get copied
Solution: Verify the path in /XD exactly matches the source structure. Use absolute paths for reliability.
Problem: Command fails with "invalid parameter"
Solution: Ensure there's no space between /XD and the directory path. The correct format is /XDpath.
For complex operations combining file and directory filters:
robocopy C:\dev\current_build C:\archive\build_2023 /XD C:\dev\current_build\temp C:\dev\current_build\logs /XF *.tmp *.bak /S /COPY:DAT /DCOPY:T /MT:8
This copies all files (except .tmp and .bak) from current_build while excluding both the temp and logs subdirectories, using multi-threading for faster performance.
When you need to copy files from a directory but exclude all its subfolders, Robocopy's exclusion parameter becomes essential. The /XD
switch allows you to specify directories to exclude from the copy operation.
The fundamental command structure looks like this:
robocopy C:\source C:\destination /XD "C:\source\subfolder1" "C:\source\subfolder2"
To exclude ALL subfolders under your target directory, use this pattern:
robocopy C:\ParentFolder C:\DestinationFolder /XD C:\ParentFolder\*
The asterisk wildcard matches all subdirectories under the specified path.
Here's a more complete example including common options:
robocopy D:\ProjectFiles E:\Backup\ProjectFiles /XD D:\ProjectFiles\* /COPY:DAT /R:3 /W:5 /MIR /LOG:C:\logs\backup.log /NP /TEE
Remember these key points when using this technique:
- The exclusion path must be absolute (full path specification)
- Wildcards only work at the end of the path
- Test with /L (dry run) first to verify the exclusion pattern
- The /MIR switch will delete files in destination - use carefully
If you specifically want only files (no folders at all), this pattern works:
robocopy C:\source C:\destination *.* /S /XF * /XD *
However, the /XD
method is generally more reliable for folder exclusion.