Skip to content

Sync-Dir

Sync's two dirs

param([string]$sourcePath = "", [string]$targetPath = "")

try {
	if ($sourcePath -eq "") { $sourcePath = Read-Host "Enter the path to the source directory" }
	if ($targetPath -eq "") { $targetPath = Read-Host "Enter the path to the target directory" }
	$stopWatch = [system.diagnostics.stopwatch]::startNew()

	"? Please wait while syncing content from ??$sourcePath to ??$targetPath ..."
	& robocopy.exe $sourcePath $targetPath /MIR /SL /FFT /NJH /NDL /NFL /NP /NS
	#
	# /MIR = mirror a directory tree
	# /SL  = copy Symbolic Links as links instead of as the link targets
	# /FFT = assume FAT file times (2-second granularity)
	# /NJH = no job header
	# /NDL = no directory list (don't log directory names)
	# /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)
	#
	if ($lastExitCode -gt 3) { throw 'Robocopy failed with exit code $lastExitCode.' }

	[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
	"? Synced ??$sourcePath to ??$targetPath in $($elapsed)s."
	exit 0 # success
} catch {
	"?? ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
	exit 1
}

Specifies the path to the source dir (to be entered by default)

Specifies the path to the target dir (to be entered by default)

An interactive directory of PowerShell scripts.