Updated sync-dir.ps1

This commit is contained in:
Markus Fleschutz 2025-02-12 09:38:37 +01:00
parent 9cee4a13f0
commit 7ff53658fd

View File

@ -1,6 +1,6 @@
<# <#
.SYNOPSIS .SYNOPSIS
Sync two dirs Sync's two dirs
.DESCRIPTION .DESCRIPTION
This PowerShell script synchronizes (mirrors) the content of 2 directory trees by using Robocopy. This PowerShell script synchronizes (mirrors) the content of 2 directory trees by using Robocopy.
Typical use cases are backups: at first everything is copied (full backup), afterward only changes are copied (incremental backup). Typical use cases are backups: at first everything is copied (full backup), afterward only changes are copied (incremental backup).
@ -11,6 +11,8 @@
Specifies the path to the target dir (to be entered by default) Specifies the path to the target dir (to be entered by default)
.EXAMPLE .EXAMPLE
PS> ./sync-dir.ps1 C:\Photos D:\Backups\Photos PS> ./sync-dir.ps1 C:\Photos D:\Backups\Photos
Please wait while syncing content from 📂C:\Photos to 📂D:\Backups\Photos ...
Synced 📂C:\Photos to 📂D:\Backups\Photos in 32s.
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -24,16 +26,18 @@ try {
if ($targetPath -eq "") { $targetPath = Read-Host "Enter the path to the target directory" } if ($targetPath -eq "") { $targetPath = Read-Host "Enter the path to the target directory" }
$stopWatch = [system.diagnostics.stopwatch]::startNew() $stopWatch = [system.diagnostics.stopwatch]::startNew()
$robocopyParameters = $sourcePath, $targetPath, '/MIR', '/FFT', '/NDL', '/NP', '/NS' "⏳ Please wait while syncing content from 📂$sourcePath to 📂$targetPath ..."
& robocopy.exe $sourcePath $targetPath /MIR /FFT /NJH /NDL /NFL /NP /NS
# #
# /MIR = mirror a directory tree # /MIR = mirror a directory tree
# /FFT = assume FAT file times (2-second granularity) # /FFT = assume FAT file times (2-second granularity)
# /NDL = don't log directory names # /NJH = no job header
# /NP = don't display percentage copied # /NDL = no directory list (don't log directory names)
# /NS = don't log file sizes # /NFL = no file list (don't log file names)
# /NP = no progress (don't display percentage copied)
# /NS = no size (don't log file sizes)
# #
& robocopy.exe $robocopyParameters if ($lastExitCode -gt 3) { throw 'Robocopy failed with exit code $lastExitCode.' }
if ($lastExitCode -gt 3) { throw 'Robocopy failed.' }
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds [int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✅ Synced 📂$sourcePath to 📂$targetPath in $($elapsed)s." "✅ Synced 📂$sourcePath to 📂$targetPath in $($elapsed)s."