[CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory = $true)] [string]$Path, [ValidateRange(1, 3650)] [int]$Days = 90, [string]$ArchiveDirectory, [switch]$Apply ) $ErrorActionPreference = 'Stop' $resolved = Resolve-Path -LiteralPath $Path -ErrorAction Stop $extensions = @('.mp3', '.wav', '.flac', '.ogg', '.m4a', '.aac', '.wma', '.opus') $singleFile = $null if (Test-Path -LiteralPath $resolved.Path -PathType Container) { $root = $resolved.Path } else { $item = Get-Item -LiteralPath $resolved.Path if ($extensions -notcontains $item.Extension.ToLowerInvariant()) { throw "Формат не похож на аудио: $($item.Extension)" } $singleFile = $item $root = Split-Path -Parent $item.FullName } $cutoff = (Get-Date).AddDays(-$Days) if ($singleFile) { $files = @($singleFile | Where-Object { $_.LastWriteTime -lt $cutoff }) } else { $files = @(Get-ChildItem -LiteralPath $root -File -Recurse -ErrorAction SilentlyContinue | Where-Object { $extensions -contains $_.Extension.ToLowerInvariant() -and $_.LastWriteTime -lt $cutoff } | Sort-Object LastWriteTime) } if ($files.Count -eq 0) { Write-Host "Записей старше $Days дней не найдено." exit 0 } if (-not $ArchiveDirectory) { $ArchiveDirectory = Join-Path $root 'archive-old' } $archiveRoot = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($ArchiveDirectory) Write-Host "Кандидатов: $($files.Count), старше $Days дней (до $($cutoff.ToString('yyyy-MM-dd')))" $moved = 0 $skipped = 0 foreach ($file in $files) { $relative = $file.FullName.Substring($root.Length).TrimStart([char[]]@([char]'\', [char]'/')) $destination = Join-Path $archiveRoot $relative Write-Host ("{0:yyyy-MM-dd} {1}" -f $file.LastWriteTime, $file.FullName) if (-not $Apply) { continue } if (Test-Path -LiteralPath $destination) { Write-Host "Пропуск: $destination уже существует" $skipped++ continue } if (-not $PSCmdlet.ShouldProcess($file.FullName, "Переместить в $destination")) { continue } $parent = Split-Path -Parent $destination New-Item -ItemType Directory -Path $parent -Force | Out-Null Move-Item -LiteralPath $file.FullName -Destination $destination $moved++ } if (-not $Apply) { Write-Host 'Это предпросмотр. Для перемещения добавьте -Apply.' } else { Write-Host "Итог: перемещено $moved, пропущено $skipped" }