[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$Destination, [switch]$Apply, [switch]$Recurse, [string[]]$Extensions = @('mp4', 'mkv', 'webm', 'mov', 'avi', 'm4v', 'ts') ) $ErrorActionPreference = 'Stop' foreach ($tool in @('ffmpeg', 'ffprobe')) { if (-not (Get-Command $tool -ErrorAction SilentlyContinue)) { throw "$tool не найден в PATH. Установите FFmpeg." } } if (-not (Test-Path -LiteralPath $Path -PathType Container)) { throw "Папка не найдена: $Path" } $root = (Resolve-Path -LiteralPath $Path).Path if (-not $Destination) { $Destination = Join-Path $root 'sorted' } $outRoot = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Destination) $wanted = $Extensions | ForEach-Object { $_.TrimStart('.').ToLowerInvariant() } $files = @( Get-ChildItem -LiteralPath $root -File -Recurse:$Recurse | Where-Object { $wanted -contains $_.Extension.TrimStart('.').ToLowerInvariant() } ) if ($files.Count -eq 0) { Write-Host 'Видеофайлы не найдены.' return } $plan = foreach ($file in $files) { $raw = & ffprobe -v error -show_entries format_tags=creation_time -of default=nw=1:nk=1 $file.FullName 2>$null $stamp = @($raw | Select-Object -First 1)[0] $date = $null if ($LASTEXITCODE -eq 0 -and $stamp) { try { $date = [datetime]::Parse( ([string]$stamp).Trim(), [Globalization.CultureInfo]::InvariantCulture, [Globalization.DateTimeStyles]::AdjustToUniversal ) } catch { $date = $null } } if ($null -eq $date) { $date = $file.LastWriteTime } $targetDir = Join-Path $outRoot $date.ToString('yyyy') $targetDir = Join-Path $targetDir $date.ToString('MM') $targetPath = Join-Path $targetDir $file.Name if (Test-Path -LiteralPath $targetPath) { Write-Warning "Пропуск, файл уже есть: $targetPath" continue } if ($Apply) { if (-not (Test-Path -LiteralPath $targetDir)) { New-Item -ItemType Directory -Path $targetDir -Force | Out-Null } Copy-Item -LiteralPath $file.FullName -Destination $targetPath Write-Host "Скопировано: $($file.Name) -> $targetDir" } [pscustomobject]@{ File = $file.FullName Date = $date.ToString('yyyy-MM-dd') Target = $targetPath Copied = [bool]$Apply } } $plan | Format-Table -AutoSize if (-not $Apply) { Write-Host 'Это предпросмотр. Для копирования добавьте -Apply.' }