Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Example: Define the folder and identify changed files
- # 20250124 revision: I added AnalyzeZip.ps1 to display a summary. If this isn't helpful you can comment or delete line 37
- # AnalyzeZip available at https://pastebin.com/wDntDwJT
- $folderPath = "c:\dayz-server\"
- $destFolder = "E:\DayZ_Backups\"
- $timestamp = Get-Date -format "yyMMddHHmm"
- $destinationZip = "$destFolder$timestamp-dayz-server-bak.zip"
- # Enumerate changed items (example: files modified in the last day)
- $sourceItems = Get-ChildItem -Path $folderPath -File -Recurse | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-1) }
- # Create an array of relative paths to preserve directory structure
- $filePaths = $sourceItems | ForEach-Object {
- # Create relative paths by trimming the root folder path
- $_.FullName.Substring($folderPath.Length).TrimStart('\')
- }
- # Use LiteralPath to include the files with relative paths in the ZIP
- if ($filePaths.Count -gt 0) {
- # Create a temporary folder to recreate the structure
- $tempFolder = Join-Path -Path $env:TEMP -ChildPath "TempZip"
- Remove-Item -Recurse -Force -Path $tempFolder -ErrorAction SilentlyContinue
- New-Item -ItemType Directory -Path $tempFolder | Out-Null
- # Copy the files into the temporary folder while preserving the relative structure
- foreach ($item in $sourceItems) {
- $relativePath = $item.FullName.Substring($folderPath.Length).TrimStart('\')
- $destination = Join-Path -Path $tempFolder -ChildPath $relativePath
- New-Item -ItemType Directory -Path (Split-Path -Path $destination -Parent) -Force | Out-Null
- Copy-Item -Path $item.FullName -Destination $destination
- }
- # Compress the recreated structure
- Compress-Archive -Path $tempFolder\* -DestinationPath $destinationZip
- # Clean up the temporary folder
- Remove-Item -Recurse -Force -Path $tempFolder
- # Display summary of number and total (uncompressed) size of files
- C:\Users\Administrator\Documents\AnalyzeZip.ps1 -zipPath $destinationZip
- } else {
- Write-Host "No files found to add to the zip archive."
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement