-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompress-FilesByMonth.ps1
More file actions
87 lines (71 loc) · 3.07 KB
/
Copy pathCompress-FilesByMonth.ps1
File metadata and controls
87 lines (71 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<#
.SYNOPSIS
Packt Dateien nach Entstehungsmonat in je ein ZIP-Archiv.
.DESCRIPTION
Gruppiert die Dateien eines Quellverzeichnisses nach Jahr und Monat und
legt je Gruppe ein Archiv im Zielverzeichnis an. Gedacht fuer
Verzeichnisse, die durch taegliche Ausgaben zulaufen - Logs, Exporte,
Scans.
.NOTES
-Confirm ist ein Pflichtparameter vom Typ bool, nicht der uebliche
PowerShell-Schalter: erst mit $true werden die Quelldateien nach dem
Packen geloescht. Zuerst mit $false laufen lassen und das Ergebnis
ansehen.
#>
function Compress-FilesByMonth {
param (
[Parameter(Mandatory = $true)]
[bool]$Confirm,
[Parameter(Mandatory = $true)]
[string]$SourceDir,
[Parameter(Mandatory = $true)]
[string]$DestDir,
[Parameter(Mandatory = $true)]
[string]$Filter
)
# Überprüfen, ob das Quellverzeichnis existiert
if (-not (Test-Path $SourceDir)) {
Write-Host "Fehler: Das Quellverzeichnis '$SourceDir' existiert nicht." -ForegroundColor Red
return
}
# Zielverzeichnis erstellen, falls es nicht existiert
if (-not (Test-Path $DestDir)) {
New-Item -ItemType Directory -Path $DestDir | Out-Null
}
# Alle passenden Dateien abrufen
$files = Get-ChildItem -Path $SourceDir -Filter $Filter -File
# Prüfen, ob Dateien vorhanden sind
if ($files.Count -eq 0) {
Write-Host "Keine passenden Dateien mit dem Filter '$Filter' gefunden im Verzeichnis '$SourceDir'." -ForegroundColor Yellow
return
}
# Dateien nach Monat gruppieren
$groups = $files | Group-Object { $_.LastWriteTime.ToString("yyyy-MM") }
foreach ($group in $groups) {
$month = $group.Name
$archiveName = Join-Path $DestDir "$month.zip"
$filesToCompress = $group.Group.FullName
try {
# Dateien komprimieren (mit Update-Option)
Compress-Archive -Path $filesToCompress -DestinationPath $archiveName -Update
Start-Sleep -Seconds 2 # Kurze Verzögerung, um sicherzustellen, dass das Archiv erstellt wurde
# Überprüfen, ob das Archiv erfolgreich erstellt wurde
if (Test-Path $archiveName) {
Write-Host "Archiv '$archiveName' erfolgreich erstellt." -ForegroundColor Green
# Dateien nach der Archivierung löschen (wenn bestätigt)
if ($Confirm) {
Remove-Item -Path $filesToCompress -Force -Confirm:$Confirm
} else {
Remove-Item -Path $filesToCompress -Force
}
Write-Host "Dateien erfolgreich gelöscht." -ForegroundColor Cyan
} else {
Write-Host "Fehler: Archiv '$archiveName' wurde nicht erstellt. Dateien werden nicht gelöscht!" -ForegroundColor Red
}
} catch {
Write-Host "Fehler beim Verarbeiten von '$month': $_" -ForegroundColor Red
}
}
}
# Beispielaufruf der Funktion
Compress-FilesByMonth -Confirm $false -SourceDir "D:\XML_TEST" -DestDir "D:\XML_TEST\gepackt" -Filter "*.bak"