From da1902517fc931b386ba42d4a23d2258b593b8a4 Mon Sep 17 00:00:00 2001 From: yuxing Date: Sun, 27 Apr 2025 09:14:54 +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 v1.2.ps1 | 139 ++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 Robocopy镜像文件小程序/MirrorFiles v1.2.ps1 diff --git a/Robocopy镜像文件小程序/MirrorFiles v1.2.ps1 b/Robocopy镜像文件小程序/MirrorFiles v1.2.ps1 new file mode 100644 index 0000000..96bcea6 --- /dev/null +++ b/Robocopy镜像文件小程序/MirrorFiles v1.2.ps1 @@ -0,0 +1,139 @@ +param( + [string]$SourcePath, + [string]$TargetPath +) + +Add-Type -AssemblyName System.Windows.Forms +Add-Type -AssemblyName System.Drawing + +function Show-GUI { + $form = New-Object System.Windows.Forms.Form + $form.Text = "Folder Mirror Tool" + $form.Size = New-Object System.Drawing.Size(600,450) # Increased height + $form.StartPosition = "CenterScreen" + $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog # Disable resizing + + # Source Controls + $lblSource = New-Object Windows.Forms.Label + $lblSource.Location = New-Object Drawing.Point(10,10) + $lblSource.Text = "Source Path:" + $txtSource = New-Object Windows.Forms.TextBox + $txtSource.Location = New-Object Drawing.Point(120,10) + $txtSource.Size = New-Object Drawing.Size(350,20) + $btnSource = New-Object Windows.Forms.Button + $btnSource.Location = New-Object Drawing.Point(480,10) + $btnSource.Text = "Browse" + $btnSource.Add_Click({ + $folder = New-Object Windows.Forms.FolderBrowserDialog + if($folder.ShowDialog() -eq "OK") { $txtSource.Text = $folder.SelectedPath } + }) + + # Target Controls + $lblTarget = New-Object Windows.Forms.Label + $lblTarget.Location = New-Object Drawing.Point(10,40) + $lblTarget.Text = "Target Path:" + $txtTarget = New-Object Windows.Forms.TextBox + $txtTarget.Location = New-Object Drawing.Point(120,40) + $txtTarget.Size = New-Object Drawing.Size(350,20) + $btnTarget = New-Object Windows.Forms.Button + $btnTarget.Location = New-Object Drawing.Point(480,40) + $btnTarget.Text = "Browse" + $btnTarget.Add_Click({ + $folder = New-Object Windows.Forms.FolderBrowserDialog + if($folder.ShowDialog() -eq "OK") { $txtTarget.Text = $folder.SelectedPath } + }) + + # Log Box + $txtLog = New-Object Windows.Forms.RichTextBox + $txtLog.Location = New-Object Drawing.Point(10,70) + $txtLog.Size = New-Object Drawing.Size(560,300) # Increased height + $txtLog.ReadOnly = $true + + # Start Button + $btnStart = New-Object Windows.Forms.Button + $btnStart.Location = New-Object Drawing.Point(480,370) # Adjusted position + $btnStart.Size = New-Object Drawing.Size(90,30) + $btnStart.Text = "Start Mirror" + $btnStart.Add_Click({ + $script:SourcePath = $txtSource.Text + $script:TargetPath = $txtTarget.Text + try { + # Reuse existing validation and mirror logic + if (-not (Test-Path $SourcePath)) { throw "Source path invalid" } + if (-not (Test-Path $TargetPath)) { throw "Target path invalid" } + + $txtLog.Text += "Starting mirror operation...`n" + $global:logFile = "$PSScriptRoot\MirrorLog_$(Get-Date -Format 'yyyyMMdd_HHmmss').log" + + 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 + + if ($process.ExitCode -ge 8) { + throw "Robocopy failed with exit code $($process.ExitCode)" + } + + $txtLog.Text += "Operation completed successfully!`n" + } + catch { + $txtLog.Text += "[ERROR] $_`n" + [Windows.Forms.MessageBox]::Show($_.Exception.Message, "Error") + } + }) + + # Add controls to form + $form.Controls.AddRange(@($lblSource, $txtSource, $btnSource, $lblTarget, $txtTarget, $btnTarget, $txtLog, $btnStart)) + [void]$form.ShowDialog() +} + +# Show GUI if no parameters provided +if(-not $PSBoundParameters.ContainsKey('SourcePath') -or + -not $PSBoundParameters.ContainsKey('TargetPath')) { + Show-GUI +} +else { + try { + if (-not (Test-Path $SourcePath)) { throw "Source path invalid" } + if (-not (Test-Path $TargetPath)) { throw "Target path invalid" } + + $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 + + 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