AlanTuringEnjoyer

LocalPlaylistGeneratorBallroomDance

Apr 4th, 2024 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param(
  2.     [Parameter()]
  3.     [int]$numberOfPlaylistsToGenerate = 15
  4. )
  5.  
  6. function Out-FileUtf8NoBom {
  7.  
  8.   <#
  9.   .SYNOPSIS
  10.     Outputs to a UTF-8-encoded file *without a BOM* (byte-order mark).
  11.  
  12.   .DESCRIPTION
  13.  
  14.     Mimics the most important aspects of Out-File:
  15.       * Input objects are sent to Out-String first.
  16.       * -Append allows you to append to an existing file, -NoClobber prevents
  17.         overwriting of an existing file.
  18.       * -Width allows you to specify the line width for the text representations
  19.         of input objects that aren't strings.
  20.     However, it is not a complete implementation of all Out-File parameters:
  21.       * Only a literal output path is supported, and only as a parameter.
  22.       * -Force is not supported.
  23.       * Conversely, an extra -UseLF switch is supported for using LF-only newlines.
  24.  
  25.   .NOTES
  26.     The raison d'être for this advanced function is that Windows PowerShell
  27.     lacks the ability to write UTF-8 files without a BOM: using -Encoding UTF8
  28.     invariably prepends a BOM.
  29.  
  30.     Copyright (c) 2017, 2022 Michael Klement <[email protected]> (http://same2u.net),
  31.     released under the [MIT license](https://spdx.org/licenses/MIT#licenseText).
  32.  
  33.   #>
  34.  
  35.   [CmdletBinding(PositionalBinding=$false)]
  36.   param(
  37.     [Parameter(Mandatory, Position = 0)] [string] $LiteralPath,
  38.     [switch] $Append,
  39.     [switch] $NoClobber,
  40.     [AllowNull()] [int] $Width,
  41.     [switch] $UseLF,
  42.     [Parameter(ValueFromPipeline)] $InputObject
  43.   )
  44.  
  45.   begin {
  46.  
  47.     # Convert the input path to a full one, since .NET's working dir. usually
  48.     # differs from PowerShell's.
  49.     $dir = Split-Path -LiteralPath $LiteralPath
  50.     if ($dir) { $dir = Convert-Path -ErrorAction Stop -LiteralPath $dir } else { $dir = $pwd.ProviderPath }
  51.     $LiteralPath = [IO.Path]::Combine($dir, [IO.Path]::GetFileName($LiteralPath))
  52.    
  53.     # If -NoClobber was specified, throw an exception if the target file already
  54.     # exists.
  55.     if ($NoClobber -and (Test-Path $LiteralPath)) {
  56.       Throw [IO.IOException] "The file '$LiteralPath' already exists."
  57.     }
  58.    
  59.     # Create a StreamWriter object.
  60.     # Note that we take advantage of the fact that the StreamWriter class by default:
  61.     # - uses UTF-8 encoding
  62.     # - without a BOM.
  63.     $sw = New-Object System.IO.StreamWriter $LiteralPath, $Append
  64.    
  65.     $htOutStringArgs = @{}
  66.     if ($Width) { $htOutStringArgs += @{ Width = $Width } }
  67.  
  68.     try {
  69.       # Create the script block with the command to use in the steppable pipeline.
  70.       $scriptCmd = {
  71.         & Microsoft.PowerShell.Utility\Out-String -Stream @htOutStringArgs |
  72.           . { process { if ($UseLF) { $sw.Write(($_ + "`n")) } else { $sw.WriteLine($_) } } }
  73.       }  
  74.      
  75.       $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
  76.       $steppablePipeline.Begin($PSCmdlet)
  77.     }
  78.     catch { throw }
  79.  
  80.   }
  81.  
  82.   process
  83.   {
  84.     $steppablePipeline.Process($_)
  85.   }
  86.  
  87.   end {
  88.     $steppablePipeline.End()
  89.     $sw.Dispose()
  90.   }
  91.  
  92. }
  93.  
  94. echo $numberOfPlaylistsToGenerate
  95.  
  96. $RootFolder = $CurrentFolder
  97. $RootFolder = "C:\Users\topos\Music\TanzenPlaylists"
  98.  
  99.  
  100.  
  101. $SubFolders = Get-ChildItem -Path $RootFolder -Directory -Name
  102.  
  103.  
  104.  
  105. foreach($i in 1..$numberOfPlaylistsToGenerate){
  106.     $Content = "#EXTM3U"
  107.     Foreach($SubFolder in $SubFolders)
  108.     {
  109.         $MusicList = Get-ChildItem -Path "$RootFolder/$Subfolder" -Name
  110.  
  111.         $Random = Get-Random $MusicList
  112.         $Content = "$Content`n$Subfolder/$Random"
  113.         $Content = "$Content`npause.mp3"
  114.     }
  115.     $index = "{0:d4}" -f $i
  116.     echo $Content | Out-FileUtf8NoBom "playlist$index.m3u8"
  117. }
Add Comment
Please, Sign In to add comment