Moosehead
Moosehead

Reputation: 1

Creating random mp3 file list for input to Media Player

I'm somewhat of a novice in Powershell programming, but I'm trying to create a random list of 50 mp3 files to use as input to play in Media Player. I've got the random file list part almost down, but am struggling to get to the next level. Here is what I've got:

$Files = Get-ChildItem E:\'My Music'\Pop_Rock\*\ -Recurse | where {$_.extension -eq ".mp3"} | Select-Object FullName
$Samplefiles = $Files | Get-Random -Count 50
foreach ($Song in $Samplefiles)
{
    echo $Song
    echo "C:\'Program Files (x86)'\'Windows Media Player'\wmplayer.exe $Song"
    C:\'Program Files (x86)'\'Windows Media Player'\wmplayer.exe $Song
    #Start-Sleep 10
}

This gets 50 random files from my Music directory as I want (snippet of 5 filenames retrieved)

echo $Song
E:\My Music\Pop_Rock\B\Brian Setzer\Brian Setzer - The Dirty Boogie\09 - Since I Don't Have You.mp3
E:\My Music\Pop_Rock\K\Korn\Korn - Untouchables\10 - Leave This Place.mp3
E:\My Music\Pop_Rock\C\The Cars\The Cars - Heartbeat City\02 - Looking For Love.mp3
E:\My Music\Pop_Rock\E\Elvis Presley\Elvis Presley - The Essential 60's Masters II - Command Performances\124 - Bossa …
E:\My Music\Pop_Rock\S\Starship\Starship - Love Among the Cannibals\02 - It's Not Enough.mp3 

My problem is, when I run this as a powershell script, the $Song variable does not resolve correctly.

echo $Song
E:\My Music\Pop_Rock\D\David Bowie\David Bowie - Heathen\08 - Gemini Spacecraft.mp3

echo of the command

C:\'Program Files (x86)'\'Windows Media Player'\wmplayer.exe @{FullName=E:\My Music\Pop_Rock\D\David Bowie\David Bowie - Heathen\08 - Gemini Spacecraft.mp3}

E:\My Music\Pop_Rock\V\Van Halen\Van Halen - Van Halen II\10 - Beautiful Girls.mp3

C:\'Program Files (x86)'\'Windows Media Player'\wmplayer.exe @{FullName=E:\My Music\Pop_Rock\V\Van Halen\Van Halen - Van Halen II\10 - Beautiful Girls.mp3}

E:\My Music\Pop_Rock\C\Christina Aguilera\Christina Aguilera - Stripped\06 - Infatuation.mp3

C:\'Program Files (x86)'\'Windows Media Player'\wmplayer.exe @{FullName=E:\My Music\Pop_Rock\C\Christina Aguilera\Christina Aguilera - Stripped\06 - Infatuation.mp3}

E:\My Music\Pop_Rock\W\The Who\The Who - Quadrophenia\103 - Quadrophenia.mp3

C:\'Program Files (x86)'\'Windows Media Player'\wmplayer.exe @{FullName=E:\My Music\Pop_Rock\W\The Who\The Who - Quadrophenia\103 - Quadrophenia.mp3}

E:\My Music\Pop_Rock\T\Ted Nugent\Ted Nugent - Free For All\06 - Together.mp3

C:\'Program Files (x86)'\'Windows Media Player'\wmplayer.exe @{FullName=E:\My Music\Pop_Rock\T\Ted Nugent\Ted Nugent - Free For All\06 - Together.mp3}

I've tested the command C:'Program Files (x86)''Windows Media Player'\wmplayer.exe in Powershell with a single file (like this) E:\My Music\Pop_Rock\T'Ted Nugent''Ted Nugent - Free For All\06 - Together'.mp3, and it works as I want, just not sure how to solve this variable/file path problem.

Any help appreciated.

Best regards,

Moosehead

Upvotes: 0

Views: 341

Answers (1)

Like @abraham stated - you need to expand the "FullName" property.

$Files = Get-ChildItem E:\'My Music'\Pop_Rock\*\ -Recurse | where {$_.extension -eq ".mp3"} | Select-Object FullName
$Samplefiles = $Files | Get-Random -Count 50
foreach ($Song in $Samplefiles)
{
    Write-Verbose -Message $Song
    Write-Verbose -Message ("C:\'Program Files (x86)'\'Windows Media Player'\wmplayer.exe {0}" -f $Song.FullName)
    #C:\'Program Files (x86)'\'Windows Media Player'\wmplayer.exe $Song.FullName
    #Start-Sleep 10
}

It seems we're all overthinking this. If you just want to make a playlist you can dump them to an .m3u and open that.

$Files = Get-ChildItem C:\Powershell\Scripts -Recurse | Where-Object {$_.extension -eq ".mp3"} | Select-Object -ExpandProperty FullName 
$Files | Get-Random -Count 50 |  Out-File -Encoding UTF8  tempplaylist.m3u


Test-Path .\tempplaylist.m3u
Write-Verbose -Message "C:\'Program Files (x86)'\'Windows Media Player'\wmplayer.exe tempplaylist.m3u" 
#C:\'Program Files (x86)'\'Windows Media Player'\wmplayer.exe tempplaylist.m3u

Upvotes: 1

Related Questions