每日自动压缩前一日文件夹并迁移至网络位置的脚本需求及问题排查
Fixing Your Daily Folder Compression PowerShell Script
Let's break down the issues in your current script and fix them step by step to meet your exact requirements:
Key Problems in the Original Script
- Broken date variable: You wrapped the date calculation in double quotes, which turns it into a literal string instead of executing the PowerShell command. This means
$yesterdaysfolderwas just storing the text(Get-Date).AddDays(-1).ToString('yyyyMMdd')instead of yesterday's date inyyyyMMddformat. - Incorrect 7-Zip syntax: Your command tried to create a file named
*.7zip(invalid for a zip archive) and didn't target the actual source folder path you specified in your requirements. - Malformed
Copy-Itemcommand: The syntaxCopy-Item $Source *.7zip $Destination1is invalid—you need to point directly to the compressed file you created. - Mismatched paths: The script used desktop directories, but your actual workflow requires a SAP-related source folder and a network share destination.
Corrected PowerShell Script
Here's the revised, production-ready script tailored to your needs:
# Path to 7-Zip executable (update if your server's 7-Zip is in a different location) $pathTo7zip = "C:\Program Files\7-Zip\7z.exe" # Root folder containing your daily yyyyMMdd folders $sourceRoot = "D:\usr\sap\gkretail\ucon\dataexchange\export_channel\results\export\EXPORT_SUCCESS" # Calculate yesterday's date in the required yyyyMMdd format $yesterdaysFolder = (Get-Date).AddDays(-1).ToString('yyyyMMdd') # Full path to the folder we need to compress $targetFolder = Join-Path -Path $sourceRoot -ChildPath $yesterdaysFolder # Temporary local path for the compressed zip (avoids network performance hits during compression) $localZipPath = Join-Path -Path "C:\Temp\ZIPPED" -ChildPath "$yesterdaysFolder.zip" # Network destination (formatted as a UNC path, required for accessing network shares) $networkDestination = "\\SOL000483\domain.com\GK_Transaction_Archive" # Step 1: Check if yesterday's folder exists before proceeding if (-not (Test-Path -Path $targetFolder -PathType Container)) { Write-Host "Error: Folder $yesterdaysFolder does not exist in $sourceRoot" exit 1 } # Step 2: Compress the folder with 7-Zip & $pathTo7zip a -tzip "$localZipPath" "$targetFolder\*" # Step 3: Verify the zip file was created successfully if (-not (Test-Path -Path $localZipPath -PathType Leaf)) { Write-Host "Error: Failed to create zip file at $localZipPath" exit 1 } # Step 4: Ensure the network destination exists (create it if missing) if (-not (Test-Path -Path $networkDestination -PathType Container)) { New-Item -Path $networkDestination -ItemType Directory -Force | Out-Null } # Step 5: Copy the zip to the network share Copy-Item -Path $localZipPath -Destination $networkDestination -Force # Optional: Clean up the local zip once we confirm it's on the network if (Test-Path -Path "$networkDestination\$yesterdaysFolder.zip") { Remove-Item -Path $localZipPath -Force Write-Host "Success: $yesterdaysFolder archived to network location" } else { Write-Host "Error: Failed to copy zip file to network destination" exit 1 }
What We Fixed & Improved
- Working date variable:
$yesterdaysFoldernow correctly evaluates to yesterday's date by removing the unnecessary quotes around the date expression. - Proper UNC network path: Formatted the network destination as
\\server\share\folder—the standard way to access network shares in PowerShell. - Robust error checking: Added checks to confirm the source folder exists, the zip was created, and the network path is accessible (with auto-creation if missing).
- Safe path handling: Used
Join-Pathto build file/folder paths, eliminating issues with missing slashes or invalid characters. - Optimized compression: Compresses locally first (faster than compressing directly to the network) then copies the finished zip.
- Cleanup option: Removes the local zip file only after verifying it's successfully copied to the network, saving local disk space.
Quick Notes for Deployment
- Ensure the account running this script has read permissions on the SAP source folder and write permissions on the
GK_Transaction_Archivenetwork share. - If your server's 7-Zip is installed in a non-default location, update the
$pathTo7zipvariable to match. - Schedule this script using Windows Task Scheduler to run daily at your preferred time (e.g., 2 AM when server load is low).
内容的提问来源于stack exchange,提问作者Gu55
相关产品推荐
相关产品推荐

