param( [string]$TargetPath = "basis14.py", [int]$Limit = 10 ) $ErrorActionPreference = 'Stop' $base = Split-Path -Parent $MyInvocation.MyCommand.Path $backupRoot = Join-Path $base "_auto_backups" if (!(Test-Path $backupRoot)) { Write-Host "FEHLER: Backup-Ordner nicht gefunden: $backupRoot" exit 1 } # Ziel vollqualifizieren (falls Datei nicht existiert, relativ zum Projekt) try { $targetFull = (Resolve-Path $TargetPath).Path } catch { $targetFull = (Join-Path $base $TargetPath) } # relativer Pfad (fuer exakten Match) $rel = $targetFull if ($targetFull.ToLower().StartsWith($base.ToLower())) { $rel = $targetFull.Substring($base.Length).TrimStart('\') } else { $rel = Split-Path -Leaf $targetFull } $leaf = Split-Path -Leaf $rel # Kandidaten sammeln $exact = Get-ChildItem -Path $backupRoot -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $_.FullName.ToLower().EndsWith(("\\" + $rel).ToLower()) } $cands = @() if ($exact -and $exact.Count -gt 0) { $cands = $exact } else { $cands = Get-ChildItem -Path $backupRoot -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $_.Name -ieq $leaf } } if (!$cands -or $cands.Count -eq 0) { Write-Host "FEHLER: Kein Backup gefunden fuer: $TargetPath" Write-Host "Gesucht nach rel=$rel bzw. Name=$leaf" exit 2 } $latest = $cands | Sort-Object LastWriteTime -Descending | Select-Object -First $Limit Write-Host "" Write-Host "==========================================" Write-Host " RESTORE PICK - Backups fuer: $TargetPath" Write-Host "==========================================" Write-Host "" $idx = 1 foreach ($f in $latest) { $ts = $f.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss") Write-Host ("[{0}] {1} {2}" -f $idx, $ts, $f.FullName) $idx++ } Write-Host "" $sel = Read-Host "Bitte Nummer (1-$($latest.Count)) eingeben" if (-not $sel) { Write-Host "Abbruch."; exit 0 } [int]$n = 0 if (-not [int]::TryParse($sel, [ref]$n)) { Write-Host "Ungueltige Eingabe."; exit 3 } if ($n -lt 1 -or $n -gt $latest.Count) { Write-Host "Ungueltige Nummer."; exit 3 } $chosen = $latest[$n-1] Write-Host "" Write-Host "GEWAEHLT:" Write-Host " $($chosen.FullName)" Write-Host "" # Vor Restore: Backup der aktuellen Datei falls existiert if (Test-Path $targetFull) { try { & powershell -NoProfile -ExecutionPolicy Bypass -File (Join-Path $base "_make_backup.ps1") $targetFull | Out-Null Write-Host "OK: Aktuelle Datei vorher gesichert." } catch { Write-Host "WARNUNG: Konnte aktuelle Datei nicht backupen, fahre trotzdem fort." } } else { $parent = Split-Path -Parent $targetFull if ($parent -and !(Test-Path $parent)) { New-Item -ItemType Directory -Force -Path $parent | Out-Null } } Copy-Item -Force $chosen.FullName $targetFull Write-Host "OK: Wiederhergestellt -> $targetFull"