如何为批量文件复制重命名的PowerShell脚本添加末尾执行结果汇总功能
Got it, let's fix this so you don't have to scan through tons of output to find success/failure markers. Here's a clean, scalable way to add a clear execution summary at the end of your script:
Step 1: Initialize tracking variables
First, add these at the top of your script to keep count of successes, failures, and track which files failed:
# Initialize summary tracking variables $successCount = 0 $failureCount = 0 $failedFiles = @() # Array to store paths of failed files
Step 2: Wrap file processing in a reusable function
Instead of copying the same code block for every file, encapsulate the logic in a function. This makes your script cleaner and easier to maintain, plus it lets us return a success/failure status to update our tracking:
function Copy-FileWithStatus { param( [Parameter(Mandatory=$true)] [string]$SourcePath, [Parameter(Mandatory=$true)] [string]$DestinationPath ) $cleanSource = $SourcePath.Trim() # Check if source exists if ($(Try { Test-Path $cleanSource } Catch { $false })) { Write-Host "Source file '$cleanSource' found, starting copy..." # Execute move/copy and check exit code for success mv -v -f $cleanSource $DestinationPath if ($LASTEXITCODE -eq 0) { Write-Host "Successfully copied '$cleanSource'!!!!!" return $true } else { Write-Host "Failed to copy '$cleanSource' (move command failed).XXXXX" return $false } } else { Write-Host "Source file '$cleanSource' NOT found.XXXXX" return $false } }
Note: We check $LASTEXITCODE here because even if the source exists, the mv command could fail (e.g., permission issues, destination path errors). This ensures we count actual copy failures, not just missing sources.
Step 3: Process each file with tracking
Now replace your repeated code blocks with calls to this function, updating our tracking variables based on the result:
# Example: Process your first file if (Copy-FileWithStatus -SourcePath $source1 -DestinationPath $filedest1) { $successCount++ } else { $failureCount++ $failedFiles += $source1.Trim() # Store the clean source path } # Repeat for all your files if (Copy-FileWithStatus -SourcePath $source2 -DestinationPath $filedest2) { $successCount++ } else { $failureCount++ $failedFiles += $source2.Trim() } # ... add more file processing calls here ...
Step 4: Generate the final summary
Add this at the end of your script to print a clear, concise summary:
# Print execution summary Write-Host "`n=====================================" Write-Host " Execution Summary " Write-Host "=====================================" Write-Host "Success: $successCount" Write-Host "Failures: $failureCount" if ($failedFiles.Count -gt 0) { Write-Host "Failure File Name(s): $($failedFiles -join ' ')" } else { Write-Host "Failure File Name(s): None" }
Example Output
When your script finishes, you'll get something like this:
===================================== Execution Summary ===================================== Success: 15 Failures: 2 Failure File Name(s): file7.csv file12.csv
This way you don't have to scroll through hundreds of lines of output—just jump straight to the summary at the end.
内容的提问来源于stack exchange,提问作者JaxIsland

