From 466aacdcdad34065575474e7540427bdc2579912 Mon Sep 17 00:00:00 2001 From: yuxing Date: Sun, 27 Apr 2025 08:56:15 +0800 Subject: [PATCH] =?UTF-8?q?Upload=20files=20to=20"Robocopy=E9=95=9C?= =?UTF-8?q?=E5=83=8F=E6=96=87=E4=BB=B6=E5=B0=8F=E7=A8=8B=E5=BA=8F"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Robocopy镜像文件小程序/MirrorFiles.ps1 | 49 ++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Robocopy镜像文件小程序/MirrorFiles.ps1 diff --git a/Robocopy镜像文件小程序/MirrorFiles.ps1 b/Robocopy镜像文件小程序/MirrorFiles.ps1 new file mode 100644 index 0000000..8b0309f --- /dev/null +++ b/Robocopy镜像文件小程序/MirrorFiles.ps1 @@ -0,0 +1,49 @@ +param( + [Parameter(Mandatory=$true)] + [string]$SourcePath, + + [Parameter(Mandatory=$true)] + [string]$TargetPath +) + +try { + # Validate paths + if (-not (Test-Path $SourcePath)) { + throw "Source path does not exist: $SourcePath" + } + if (-not (Test-Path $TargetPath)) { + throw "Target path does not exist: $TargetPath" + } + + # Create log file path + $logFile = "$PSScriptRoot\MirrorLog_$(Get-Date -Format 'yyyyMMdd_HHmmss').log" + + Write-Host "[$(Get-Date)] Starting mirror operation..." + Write-Host "Source: $SourcePath" + Write-Host "Target: $TargetPath" + Write-Host "Log file: $logFile" + + # Robocopy parameters: + # /MIR = Mirror mode (purge files in destination not in source) + # /NP = No progress percentage + # /NDL = No directory logging + # /LOG+: Append to log file + # /TEE = Output to console and log file + # /R:5 = 5 retries on failed files + # /W:5 = 5 second wait between retries + $robocopyArgs = @($SourcePath, $TargetPath, "/MIR", "/NP", "/NDL", "/LOG+:$logFile", "/TEE", "/R:5", "/W:5") + + $process = Start-Process robocopy -ArgumentList $robocopyArgs -NoNewWindow -PassThru -Wait + + # Robocopy exit codes: https://ss64.com/nt/robocopy-exit.html + if ($process.ExitCode -ge 8) { + throw "Robocopy failed with exit code $($process.ExitCode)" + } + + Write-Host "[$(Get-Date)] Mirror operation completed successfully" -ForegroundColor Green + Write-Host "Total processing time: $($process.ExitTime - $process.StartTime)" +} +catch { + Write-Host "[ERROR] $_" -ForegroundColor Red + exit 1 +} \ No newline at end of file