94 lines
2.7 KiB
PowerShell
94 lines
2.7 KiB
PowerShell
#requires -Version 5.1
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
$ProjectRoot = Split-Path -Parent $PSScriptRoot
|
|
$RootPath = Join-Path $ProjectRoot "downloads"
|
|
$Separator = " - "
|
|
|
|
try {
|
|
$Host.UI.RawUI.WindowTitle = "Группировка альбомов по исполнителям"
|
|
}
|
|
catch {
|
|
}
|
|
|
|
function Wait-BeforeExit {
|
|
Write-Host ""
|
|
[void](Read-Host "Нажмите Enter для выхода")
|
|
}
|
|
|
|
Clear-Host
|
|
|
|
Write-Host "Группировка альбомов по исполнителям" -ForegroundColor Cyan
|
|
Write-Host "Папка: $RootPath"
|
|
Write-Host ""
|
|
|
|
if (-not (Test-Path -LiteralPath $RootPath -PathType Container)) {
|
|
New-Item -ItemType Directory -Path $RootPath -Force | Out-Null
|
|
}
|
|
|
|
$AlbumFolders = Get-ChildItem -LiteralPath $RootPath -Directory |
|
|
Where-Object {
|
|
$_.Name.Contains($Separator)
|
|
} |
|
|
ForEach-Object {
|
|
$Artist = ($_.Name -split [regex]::Escape($Separator), 2)[0].Trim()
|
|
|
|
[PSCustomObject]@{
|
|
Artist = $Artist
|
|
Folder = $_
|
|
}
|
|
}
|
|
|
|
$ArtistGroups = $AlbumFolders | Group-Object -Property Artist
|
|
|
|
$Moved = 0
|
|
$Skipped = 0
|
|
|
|
foreach ($Group in $ArtistGroups) {
|
|
# Папка исполнителя создаётся только при наличии двух и более альбомов.
|
|
if ($Group.Count -lt 2) {
|
|
continue
|
|
}
|
|
|
|
$ArtistName = $Group.Name
|
|
$ArtistPath = Join-Path -Path $RootPath -ChildPath $ArtistName
|
|
|
|
if (-not (Test-Path -LiteralPath $ArtistPath -PathType Container)) {
|
|
New-Item -Path $ArtistPath -ItemType Directory | Out-Null
|
|
Write-Host "Создана папка: $ArtistName" -ForegroundColor Green
|
|
}
|
|
|
|
foreach ($Album in $Group.Group) {
|
|
$SourcePath = $Album.Folder.FullName
|
|
$DestinationPath = Join-Path -Path $ArtistPath -ChildPath $Album.Folder.Name
|
|
|
|
if (Test-Path -LiteralPath $DestinationPath) {
|
|
Write-Warning "Папка уже существует, пропущено: $DestinationPath"
|
|
$Skipped++
|
|
continue
|
|
}
|
|
|
|
try {
|
|
Move-Item `
|
|
-LiteralPath $SourcePath `
|
|
-Destination $ArtistPath `
|
|
-ErrorAction Stop
|
|
|
|
Write-Host "Перемещено: $($Album.Folder.Name) -> $ArtistName" -ForegroundColor Cyan
|
|
$Moved++
|
|
}
|
|
catch {
|
|
Write-Warning "Не удалось переместить '$SourcePath': $($_.Exception.Message)"
|
|
$Skipped++
|
|
}
|
|
}
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "Обработка завершена." -ForegroundColor Yellow
|
|
Write-Host "Перемещено: $Moved"
|
|
Write-Host "Пропущено: $Skipped"
|
|
|
|
Wait-BeforeExit
|