[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [switch]$Apply ) $ErrorActionPreference = 'Stop' $resolved = Resolve-Path -LiteralPath $Path -ErrorAction Stop if (-not (Test-Path -LiteralPath $resolved -PathType Container)) { throw "Укажите папку сериала: $resolved" } $pattern = '^(?i)(?:season[ ._-]*|s)(\d{1,2})$' $directories = Get-ChildItem -LiteralPath $resolved -Directory -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.Name -match $pattern } $plan = foreach ($directory in $directories) { if ($directory.Name -match $pattern) { $number = [int]$Matches[1] } else { continue } $newName = 'Season {0:D2}' -f $number if ($directory.Name -eq $newName) { continue } $target = Join-Path $directory.Parent.FullName $newName [pscustomobject]@{ Current = $directory.FullName NewName = $newName Target = $target Conflict = Test-Path -LiteralPath $target } } if (-not $plan) { Write-Host 'Папки сезонов уже в едином формате или не найдены.' exit 0 } $plan | Format-Table -AutoSize foreach ($item in $plan) { if ($item.Conflict) { Write-Warning "Пропуск, цель уже существует: $($item.Target)" continue } if ($Apply) { Rename-Item -LiteralPath $item.Current -NewName $item.NewName Write-Host "Переименовано: $($item.Current) -> $($item.NewName)" } else { Write-Host "Будет: $($item.Current) -> $($item.NewName)" } } if (-not $Apply) { Write-Host 'Это предпросмотр. Для применения добавьте -Apply.' }