Created
August 11, 2024 17:49
Sync folder functions using Robocopy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Sync-FolderUsingRobocopy | |
{ | |
[CmdletBinding()] | |
param( | |
# Source Path | |
[Parameter(Mandatory = $true)] | |
[string]$Source, | |
# Destination Path | |
[Parameter(Mandatory = $true)] | |
[string]$Destination, | |
# Log file Path | |
[Parameter(Mandatory = $false)] | |
[string]$LogFile = 'C:\Temp\RobocopyLog.txt' | |
) | |
# Robocopy params | |
$RobocopyParams = ('/MIR /W:0 /R:0 /NP /TEE /MT:16').Split(' ') | |
<# | |
/E :: copy subdirectories, including Empty ones. IMPORTANT ! NE pas mettre /MIR, car si un user supprime un fichier à la source il sera supprimé à la cible | |
/R:n :: number of Retries on failed copies: default 1 million. /R:0 = Ne pas réessayer. Si un fichier est locké, on l'aura le lendemain | |
/W:n :: Wait time between retries: default is 30 seconds. R:0 = Ne pas attendre | |
/NP :: No Progress - don't display percentage copied. | |
/TEE :: output to console window, as well as the log file. | |
/LOG:file :: output status to LOG file (overwrite existing log). On fera 1 fichier de log distinct à chaque fois | |
/MT[:n] :: Perform multi-threaded copies with n threads (default is 8). n must be at least 1 and not greater than 128. This option is incompatible with the /IPG and /EFSRAW options. | |
#> | |
# LogFile Arguments | |
$LogFileArgs = "/LOG:$Logfile" | |
# Exécuting Robocopy command | |
& Robocopy.exe $Source $Destination $RobocopyParams $LogFileArgs | |
#region comment | |
<# Exit Code Reference | |
Source : https://learn.microsoft.com/en-us/troubleshoot/windows-server/backup-and-storage/return-codes-used-robocopy-utility | |
0 No files were copied. No failure was met. No files were mismatched. The files already exist in the destination directory; so the copy operation was skipped. | |
1 All files were copied successfully. | |
2 There are some additional files in the destination directory that aren't present in the source directory. No files were copied. | |
3 Some files were copied. Additional files were present. No failure was met. | |
5 Some files were copied. Some files were mismatched. No failure was met. | |
6 Additional files and mismatched files exist. No files were copied and no failures were met. Which means that the files already exist in the destination directory. | |
7 Files were copied, a file mismatch was present, and additional files were present. | |
8 Several files didn't copy. | |
#> | |
#endregion comment | |
# Checking the exit code and log file | |
if ($LASTEXITCODE -le 3) | |
{ | |
Write-Host '- No errors detected, deleting log file' -ForegroundColor Green | |
Remove-Item $LogFile | |
Write-Host '- Synchronization completed successfully.' -ForegroundColor Green | |
} | |
else | |
{ | |
Write-Host '- One or more errors detected, log file displayed' -ForegroundColor Cyan | |
Write-Host '- Synchronization encountered errors. Please check the log file : ', "[$LogFile]" -ForegroundColor cyan | |
} | |
} | |
<# | |
example of use | |
Write-Host 'Sync Useful-InPowershell ==> External Media' -ForegroundColor Magenta | |
Sync-FolderUsingRobocopy -Source 'D:\Useful-InPowershell' -Destination 'E:\Useful-InPowershell' | |
Write-Host 'Sync Recover to Sort ==> External Media' -ForegroundColor Magenta | |
Sync-FolderUsingRobocopy -Source 'D:\Recup A trier' -Destination 'E:\RECUP A TRIER' | |
... | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment