Robocopy: Overwrite All Files Except Newer Destination Files – Preserving User Modifications


2 views

After restoring a file share from backup, I discovered some files were corrupt while users continued modifying other files. The challenge: overwrite all files from backup except those that users have modified (newer timestamp).

The solution lies in using robocopy's time comparison flags. The /XN (exclude newer) switch is key:

robocopy "\\backup\share" "\\production\share" /E /COPYALL /XN /XO /R:3 /W:5 /LOG:C:\robocopy.log
  • /XN - Exclude files newer than source (protects user modifications)
  • /XO - Exclude older files (though redundant with /XN in this case)
  • /COPYALL - Copy all file info (timestamps, attributes, etc.)
  • /E - Copy subdirectories including empty ones

Imagine this directory structure before running robocopy:

Backup:
File1.txt (2023-01-01)
File2.txt (2023-01-01)

Production:
File1.txt (2023-01-01) [corrupt]
File2.txt (2023-01-05) [user modified]

The command would:

  • Overwrite File1.txt (same timestamp in backup)
  • Skip File2.txt (newer in production)

While you mentioned /IS, note it includes same files (doesn't exclude newer). For completeness:

robocopy source dest *.* /IS /XN

This would still protect newer files while including identical files (which might be necessary in some scenarios).

Always include logging for verification:

/LOG+:C:\robocopy.log /TEE /NP /NDL

The /LOG+ appends to existing logs, while /TEE outputs to both console and log file.

For network shares with intermittent connectivity issues:

/R:5 /W:10 /REG /MIR /ZB

This adds retry logic (5 attempts waiting 10 seconds between), registry-based restart capability, and mirror mode with backup mode.


When dealing with file synchronization after a backup restoration, we often encounter scenarios where:

  • Some restored files might be corrupted while backups remain intact
  • Users continue modifying files during the recovery process
  • We need to selectively overwrite files without losing recent changes

The key to solving this lies in Robocopy's timestamp comparison capabilities. The /XO (eXclude Older) switch is particularly useful:

robocopy source destination /XO /COPYALL /E /R:1 /W:1

This command will copy files from source to destination, excluding files where the destination is newer.

Switch Description
/XO Exclude older files (skip if destination is newer)
/XN Exclude newer files (opposite of what we want)
/XX Exclude extra files (not relevant here)
/IS Include same files (overwrites identical files)

For a complete solution that handles permissions and retries:

robocopy \\server\share\source \\server\share\destination /MIR /XO /COPY:DATSOU /DCOPY:T /R:3 /W:5 /LOG:C:\robocopy.log /TEE

Breaking this down:

  • /MIR - Mirror mode (careful with this in production)
  • /COPY:DATSOU - Copy all attributes including security
  • /DCOPY:T - Copy directory timestamps
  • /R:3 - Retry 3 times
  • /W:5 - Wait 5 seconds between retries
  • /LOG - Create output log
  • /TEE - Output to console and log

For more granular control, consider this PowerShell script:


$source = "C:\source"
$dest = "C:\destination"

Get-ChildItem $source -Recurse | ForEach-Object {
$destFile = Join-Path $dest $_.FullName.Substring($source.Length)
if (Test-Path $destFile) {
if ($_.LastWriteTime -gt (Get-Item $destFile).LastWriteTime) {
Copy-Item $_.FullName -Destination $destFile -Force
}
}
else {
Copy-Item $_.FullName -Destination $destFile -Force
}
}

Always test with /L (list-only) mode first:

robocopy source destination /XO /L /NP /LOG:C:\test.log

This will show what would be copied without actually making changes.

  • Using /MIR without /XO will delete newer files in destination
  • Not using /COPYALL might miss security attributes
  • Forgetting /R and /W could cause failures on busy networks
  • Missing /TEE makes monitoring progress difficult