[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$SeriesName, [switch]$Apply ) $ErrorActionPreference = 'Stop' $resolved = Resolve-Path -LiteralPath $Path -ErrorAction Stop if (-not (Test-Path -LiteralPath $resolved -PathType Container)) { throw "Укажите папку сериала: $resolved" } if (-not $SeriesName) { $SeriesName = Split-Path -Leaf $resolved } $videos = Get-ChildItem -LiteralPath $resolved -File -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.Extension -in '.mkv', '.mp4', '.avi', '.mov', '.m4v' } $pattern = '(?i)S(?\d{1,2})[ ._-]*E(?\d{1,3})' $plan = foreach ($file in $videos) { if ($file.Name -match $pattern) { $season = [int]$Matches['season'] $episode = [int]$Matches['episode'] } else { continue } $tail = $file.BaseName.Substring($Matches[0].Length).Trim([char[]]' -._') $suffix = if ($tail) { " - $tail" } else { '' } $newName = ('{0} - S{1:D2}E{2:D2}{3}{4}' -f $SeriesName, $season, $episode, $suffix, $file.Extension) $newName = $newName -replace '[<>:"/\\|?*]', '' $target = Join-Path $file.DirectoryName $newName if ($target -eq $file.FullName) { continue } [pscustomobject]@{ Current = $file.FullName NewName = $newName Target = $target Conflict = Test-Path -LiteralPath $target } } if (-not $plan) { Write-Host 'Файлы с номерами SxxEyy не найдены.' exit 0 } $plan | Format-Table Current, NewName, Conflict -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.NewName)" } else { Write-Host "Будет: $(Split-Path -Leaf $item.Current) -> $($item.NewName)" } } if (-not $Apply) { Write-Host 'Это предпросмотр. Для применения добавьте -Apply.' }