Skip to content

Instantly share code, notes, and snippets.

@KillyMXI
Created April 29, 2021 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KillyMXI/f36349424e3215e535d19fc9fe4b18ef to your computer and use it in GitHub Desktop.
Save KillyMXI/f36349424e3215e535d19fc9fe4b18ef to your computer and use it in GitHub Desktop.
Shuffle groups in an AIMP playlist ("*.aimppl4") while preserving the order inside groups.
<#
.SYNOPSIS
Shuffle groups in an AIMP playlist ("*.aimppl4")
while preserving the order inside groups.
.PARAMETER inputPath
Input file to be shuffled ("*.aimppl4").
#>
Param (
[Parameter(Mandatory, HelpMessage="Input file to be shuffled.")]
[ValidateScript({($_ -Like "*.aimppl4") -And (Test-Path $_ -PathType Leaf)})]
[string]$inputPath
)
Set-StrictMode -Version Latest
$lines = Get-Content $inputPath
$contentMarkerLine = $lines |
Select-String -pattern "`#-----CONTENT-----`#" -SimpleMatch |
Select-Object -ExpandProperty LineNumber
$groupsList = [System.Collections.ArrayList]@()
$group = [System.Collections.ArrayList]@()
$lines |
Select-Object -Skip $contentMarkerLine |
ForEach-Object -Process {
If ($_.StartsWith("-") -And ($group.Count -gt 0)) {
[void]$groupsList.Add($group)
$group = [System.Collections.ArrayList]@()
}
[void]$group.Add($_)
}
[void]$groupsList.Add($group)
Write-Host "Found $($groupsList.Count) groups."
$shuffled =
($lines | Select-Object -First $contentMarkerLine) +
@($groupsList | Sort-Object { Get-Random } | ForEach-Object { $_ })
$shuffled | Set-Content $inputPath -Encoding unicode
Write-Host "Shuffled."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment