Загрузка данных


$root = "$env:USERPROFILE\кино"

$seriesRoot = Join-Path $root "Сериалы"
$moviesRoot = Join-Path $root "Фильмы"

New-Item -ItemType Directory -Force -Path $seriesRoot | Out-Null
New-Item -ItemType Directory -Force -Path $moviesRoot | Out-Null

# ----------------------------
# Перенос сериалов
# ----------------------------

$swatyOld = Join-Path $root "сваты маме"
$swatyNew = Join-Path $seriesRoot "Сваты (2008)"

if (Test-Path $swatyOld) {
    Move-Item $swatyOld $swatyNew
}

$holidaysOld = Join-Path $root "праздники"
$holidaysNew = Join-Path $seriesRoot "Праздники (2023)"

if (Test-Path $holidaysOld) {
    Move-Item $holidaysOld $holidaysNew
}

# ----------------------------
# Переименование серий
# ----------------------------

$shows = @(
    @{
        Path = $swatyNew
        Name = "Сваты (2008)"
    },
    @{
        Path = $holidaysNew
        Name = "Праздники (2023)"
    }
)

foreach ($show in $shows) {

    if (-not (Test-Path $show.Path)) {
        continue
    }

    $seasonDirs = Get-ChildItem $show.Path -Directory

    foreach ($seasonDir in $seasonDirs) {

        if ($seasonDir.Name -notmatch '^(\d+)\s*сезон$') {
            continue
        }

        $season = [int]$matches[1]

        Get-ChildItem $seasonDir.FullName -File -Filter "*.mp4" | ForEach-Object {

            if ($_.BaseName -match '^(\d+)\s*серия(?:\s*(.*))?$') {

                $episode = [int]$matches[1]
                $title = $matches[2]

                if ($title) {
                    $newName = "{0} S{1:D2}E{2:D2} {3}.mp4" -f `
                        $show.Name, $season, $episode, $title
                }
                else {
                    $newName = "{0} S{1:D2}E{2:D2}.mp4" -f `
                        $show.Name, $season, $episode
                }

                Rename-Item $_.FullName -NewName $newName
            }
        }

        Rename-Item `
            $seasonDir.FullName `
            -NewName ("Season {0:D2}" -f $season)
    }
}

# ----------------------------
# Фильм
# ----------------------------

$movie = Get-ChildItem $root -File -Filter "*.mp4" |
    Where-Object { $_.BaseName -like "Здесь был Юра*" } |
    Select-Object -First 1

if ($movie) {

    $movieDir = Join-Path $moviesRoot "Здесь был Юра (2025)"
    New-Item -ItemType Directory -Force -Path $movieDir | Out-Null

    Move-Item `
        $movie.FullName `
        (Join-Path $movieDir "Здесь был Юра (2025).mp4")
}

Write-Host ""
Write-Host "Готово."
Write-Host "MP4-файлов:" (Get-ChildItem $root -Recurse -File -Filter "*.mp4").Count