Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param(
- [Parameter()]
- [int]$numberOfPlaylistsToGenerate = 15
- )
- function Out-FileUtf8NoBom {
- <#
- .SYNOPSIS
- Outputs to a UTF-8-encoded file *without a BOM* (byte-order mark).
- .DESCRIPTION
- Mimics the most important aspects of Out-File:
- * Input objects are sent to Out-String first.
- * -Append allows you to append to an existing file, -NoClobber prevents
- overwriting of an existing file.
- * -Width allows you to specify the line width for the text representations
- of input objects that aren't strings.
- However, it is not a complete implementation of all Out-File parameters:
- * Only a literal output path is supported, and only as a parameter.
- * -Force is not supported.
- * Conversely, an extra -UseLF switch is supported for using LF-only newlines.
- .NOTES
- The raison d'être for this advanced function is that Windows PowerShell
- lacks the ability to write UTF-8 files without a BOM: using -Encoding UTF8
- invariably prepends a BOM.
- Copyright (c) 2017, 2022 Michael Klement <[email protected]> (http://same2u.net),
- released under the [MIT license](https://spdx.org/licenses/MIT#licenseText).
- #>
- [CmdletBinding(PositionalBinding=$false)]
- param(
- [Parameter(Mandatory, Position = 0)] [string] $LiteralPath,
- [switch] $Append,
- [switch] $NoClobber,
- [AllowNull()] [int] $Width,
- [switch] $UseLF,
- [Parameter(ValueFromPipeline)] $InputObject
- )
- begin {
- # Convert the input path to a full one, since .NET's working dir. usually
- # differs from PowerShell's.
- $dir = Split-Path -LiteralPath $LiteralPath
- if ($dir) { $dir = Convert-Path -ErrorAction Stop -LiteralPath $dir } else { $dir = $pwd.ProviderPath }
- $LiteralPath = [IO.Path]::Combine($dir, [IO.Path]::GetFileName($LiteralPath))
- # If -NoClobber was specified, throw an exception if the target file already
- # exists.
- if ($NoClobber -and (Test-Path $LiteralPath)) {
- Throw [IO.IOException] "The file '$LiteralPath' already exists."
- }
- # Create a StreamWriter object.
- # Note that we take advantage of the fact that the StreamWriter class by default:
- # - uses UTF-8 encoding
- # - without a BOM.
- $sw = New-Object System.IO.StreamWriter $LiteralPath, $Append
- $htOutStringArgs = @{}
- if ($Width) { $htOutStringArgs += @{ Width = $Width } }
- try {
- # Create the script block with the command to use in the steppable pipeline.
- $scriptCmd = {
- & Microsoft.PowerShell.Utility\Out-String -Stream @htOutStringArgs |
- . { process { if ($UseLF) { $sw.Write(($_ + "`n")) } else { $sw.WriteLine($_) } } }
- }
- $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
- $steppablePipeline.Begin($PSCmdlet)
- }
- catch { throw }
- }
- process
- {
- $steppablePipeline.Process($_)
- }
- end {
- $steppablePipeline.End()
- $sw.Dispose()
- }
- }
- echo $numberOfPlaylistsToGenerate
- $RootFolder = $CurrentFolder
- $RootFolder = "C:\Users\topos\Music\TanzenPlaylists"
- $SubFolders = Get-ChildItem -Path $RootFolder -Directory -Name
- foreach($i in 1..$numberOfPlaylistsToGenerate){
- $Content = "#EXTM3U"
- Foreach($SubFolder in $SubFolders)
- {
- $MusicList = Get-ChildItem -Path "$RootFolder/$Subfolder" -Name
- $Random = Get-Random $MusicList
- $Content = "$Content`n$Subfolder/$Random"
- $Content = "$Content`npause.mp3"
- }
- $index = "{0:d4}" -f $i
- echo $Content | Out-FileUtf8NoBom "playlist$index.m3u8"
- }
Add Comment
Please, Sign In to add comment