# Rename-dkbkonto.ps1 # # Die DKB-Bank hat in den letzten Jahren drei Schreibweisen für die PDF-Kontoauszüge verwendet. # Dieses Skript benennt die Dateien in ein einheitliches Format um. # # Bisherige Formate: # A: Kontoauszug_1234567890_Nr_2024_001_per_05.01.2024 # B: 20240605_Kontoauszug_6_2024_vom_05.06.2024_zu_Konto_1234567890 # C: 2024-09-05_Kontoauszug_9_2024_vom_05.09.2024_zu_Konto_1234567890 # Zielformat Kontoauszug_1234567890_2024_001_vom_05.01.2024 param( [Parameter(Mandatory=$true)] [string]$path = "." ) $files = Get-ChildItem -Path $path -Filter "*.pdf" -Recurse foreach ($file in $files) { Write-host "File: $($file.basename)" -NoNewline switch -regex ($file.name) { 'Kontoauszug_(\d+)_(\d{4})_(\d{3})_vom_(\d{2}).(\d{2}).(\d{4})' { # Zielformat Kontoauszug_1234567890_2024_001_vom_05.01.2024 Write-Host " - Correct Format" -ForegroundColor Green } 'Kontoauszug_(\d+)_Nr_(\d{4})_(\d{3})_per_(\d{4})_(\d{2})_(\d{2})' { # A: Kontoauszug_1234567890_Nr_2023_009_per_2023_09_05 $newname = "Kontoauszug_$($matches[1])_$($matches[2])_$(([long]$matches[3]).tostring('000'))_vom_$($matches[6]).$($matches[5]).$($matches[4])" Write-Host " - Match1`n`f Newname: $($newname)" -ForegroundColor Blue # Zielformat Kontoauszug_1234567890_2024_001_vom_05.01.2024 } '(\d{4})(\d{2})(\d{2})_Kontoauszug_(\d+)_(\d+)_vom_(\d{2}\.\d{2}\.\d{4})_zu_Konto_(\d+)' { # B: 20240605_Kontoauszug_6_2024_vom_05.06.2024_zu_Konto_1234567890 $newname = "Kontoauszug_$($matches[7])_$($matches[5])_$(([long]$matches[4]).tostring('000'))_vom_$($matches[6])" Write-Host " - Match2`n`f Newname: $($newname)" -ForegroundColor Blue # Zielformat Kontoauszug_1234567890_2024_001_vom_05.01.2024 } '(\d{4})-(\d{2})-(\d{2})_Kontoauszug_(\d+)_(\d+)_vom_(\d{2}\.\d{2}\.\d{4})_zu_Konto_(\d+)' { # C: 2024-09-05_Kontoauszug_9_2024_vom_05.09.2024_zu_Konto_1234567890 $newname = "Kontoauszug_$($matches[7])_$($matches[5])_$(([long]$matches[4]).tostring('000'))_vom_$($matches[6])" Write-Host " - Match3`n`f Newname: $($newname)" -ForegroundColor Blue # Zielformat Kontoauszug_1234567890_2024_001_vom_05.01.2024 } 'Kontoauszug_(\d+)_(\d{4})_vom_(\d{2}\.\d{2}\.\d{4})_zu_Konto_(\d+)' { # D: Kontoauszug_2_2024_vom_05.04.2024_zu_Konto_1234567890 $newname = "Kontoauszug_$($matches[4])_$($matches[2])_$(([long]$matches[1]).tostring('000'))_vom_$($matches[3])" Write-Host " - Match4`n`f Newname: $($newname)" -ForegroundColor Blue # Zielformat Kontoauszug_1234567890_2024_001_vom_05.01.2024 } 'Kontoauszug_(\d+)_(\d{4})_(\d{3})_vom_(\d{4})_(\d{2})_(\d{2})' { # E: Kontoauszug_1234567890_2023_006_vom_2023_11_06 $newname = "Kontoauszug_$($matches[1])_$($matches[2])_$($matches[3])_vom_$($matches[6]).$($matches[5]).$($matches[4])" Write-Host " - Match5`n`f Newname: $($newname)" -ForegroundColor Blue # Zielformat Kontoauszug_1234567890_2024_001_vom_05.01.2024 } 'Kontoauszug_(\d+)_Nr_(\d{4})_(\d{3})_vom_(\d{2})\.(\d{2})\.(\d{4})' { # F: Kontoauszug_1234567890_Nr_2024_004_vom_05.04.2024 $newname = "Kontoauszug_$($matches[1])_$($matches[2])_$($matches[3])_vom_$($matches[4]).$($matches[5]).$($matches[6])" Write-Host " - Match5`n`f Newname: $($newname)" -ForegroundColor Blue # Zielformat Kontoauszug_1234567890_2024_001_vom_05.01.2024 } 'Kontoauszug_(\d+)_Nr_(\d{4})_(\d{3})_per_(\d{4})\.(\d{2})\.(\d{2})' { # A: Kontoauszug_1234567890_Nr_2023_009_per_2023.09.05 $newname = "Kontoauszug_$($matches[1])_$($matches[2])_$(([long]$matches[3]).tostring('000'))_vom_$($matches[6]).$($matches[5]).$($matches[4])" Write-Host " - Match6`n`f Newname: $($newname)" -ForegroundColor Blue # Zielformat Kontoauszug_1234567890_2024_001_vom_05.01.2024 } 'Kontoauszug_(\d+)_Nr_(\d{4})_(\d{3})_per_(\d{2})\.(\d{2})\.(\d{4})' { # A: Kontoauszug_1234567890_Nr_2023_013_per_05.01.2024 $newname = "Kontoauszug_$($matches[1])_$($matches[2])_$(([long]$matches[3]).tostring('000'))_vom_$($matches[4]).$($matches[5]).$($matches[6])" Write-Host " - Match7`n`f Newname: $($newname)" -ForegroundColor Blue # Zielformat Kontoauszug_1234567890_2024_001_vom_05.01.2024 } default { Write-Host " - No match" -ForegroundColor Yellow $newname = $null } } if ($null -ne $newname) { $newname = $newname + $file.extension # Sicherheitshalber prüfe ich, ob die Datei schon existiert if (-not (Test-Path -Path $newname)) { Write-Host "Rename File:$($file.name)" Write-Host "New Name :$($newname)" Rename-Item -Path $file.FullName -NewName $newname } else { Write-Host "File $($newname) already exists - Skip" -ForegroundColor Yellow } } }