When using Robocopy's /MOVE parameter to transfer files between directories, many administrators encounter an unexpected behavior: the source directory itself gets deleted after the operation completes. This occurs because /MOVE combines /COPY and /PURGE operations.
// Problematic command example:
robocopy "C:\source\folder" "C:\destination\folder" *.* /S /MOVE
To maintain the source directory structure while moving contents, we need to modify our approach:
// Correct implementation:
robocopy "C:\source\folder" "C:\destination\folder" *.* /S /E /COPYALL /DCOPY:T /R:1 /W:1
Here's what each switch accomplishes:
/S- Copy subdirectories (except empty ones)/E- Copy subdirectories (including empty ones)/COPYALL- Copy all file information/DCOPY:T- Copy directory timestamps/R:1- Retry once on failed copies/W:1- Wait 1 second before retries
After copying, you can optionally remove the contents (but not the folder itself) from the source:
// Remove contents while preserving folder structure:
del /q "C:\source\folder\*.*" /s /f
for /d %x in ("C:\source\folder\*") do @rd /s /q "%x"
Consider this real-world scenario for merging project folders:
@echo off
SET src="D:\Projects\LegacySystem"
SET dst="D:\Projects\CurrentSystem"
:: Copy all contents without removing source folder
robocopy %src% %dst% *.* /S /E /COPYALL /DCOPY:T /R:3 /W:5 /LOG+:C:\Temp\merge_log.txt
:: Verify copy operation
if %ERRORLEVEL% LSS 8 (
echo Successfully merged contents
:: Optional: Clear source contents
del /q %src%\*.* /s /f
for /d %%x in (%src%\*) do @rd /s /q "%%x"
) else (
echo Merge operation encountered errors
)
For more complex scenarios, consider these approaches:
// Using PowerShell alternative:
Get-ChildItem "C:\source\folder" | Move-Item -Destination "C:\destination\folder"
// Using xcopy for simpler cases:
xcopy "C:\source\folder\*" "C:\destination\folder\" /E /H /C /I /Y
When dealing with large directory structures:
- Add
/MT:16for multi-threaded copies (16 threads) - Use
/LOG+:pathto maintain operation logs - Consider
/Zfor restartable mode in case of interruptions - Add
/NPto minimize console output overhead
When using Robocopy with the /MOVE flag, many developers encounter an unexpected behavior: the entire source directory gets deleted after the transfer completes. This happens because /MOVE is essentially a /COPY followed by a /PURGE operation.
Instead of using /MOVE, we can achieve the desired result by combining other Robocopy switches:
robocopy "C:\source\folder" "C:\destination\folder" * /S /E /COPYALL /R:1 /W:1
After the copy completes, manually delete the files (but not the directory) from the source:
del /Q "C:\source\folder\*.*"
for /D %x in ("C:\source\folder\*") do @rd /S /Q "%x"
Another method involves using a temporary intermediate directory:
robocopy "C:\source\folder" "C:\temp\holding" * /S /MOVE
robocopy "C:\temp\holding" "C:\destination\folder" * /S /E /COPYALL
- Always test with
/L(list-only) switch first - Consider using
/LOG+:filenamefor audit trails - For large transfers, add
/MTfor multi-threading - Use
/XJDto exclude junction points if needed
Here's how to move user profile data while preserving folder structure:
robocopy "C:\Users\OldProfile\Documents" "C:\Users\NewProfile\Documents" * /S /E /COPYALL /ZB /R:3 /W:5 /TEE /LOG+:C:\Migration.log