1

I am very new to powershell and am just beginning to learn the depth of this progrm. My problem is I am getting a ton of information that I am having trouble grasping all of it and so many conflicting methods I am getting lost. This is what I have tried so far and the error message I am getting. If anyone can tell me what I am doing wrong and at least give a hint on how to fix it I would be very happy. Thanks in advance.

Script

$PlaylistName = Read-Host 'Playlist filename (Include drive and path):'
$Directory = Read-Host 'Target Directory (Include drive and path):'
Write-Host "Searching "$PlaylistName" for media files and copying to "$Directory"`n"
Get-Content $PlaylistName | where {$+.trim() -ne "" } | Out-File Temp.txt
get-content Temp.txt | select-string -pattern "#EXTINF:0" -notmatch | Out-File Musiclist.txt

The playlist is here: https://gist.github.com/zipster1967/ddc5ce0d81e70f3e59cf0dfb2b224704

When I run this script I get the following error message and I am not sure what it means.

At line:4 char:44 + Get-Content $PlaylistName | where {$+.trim() -ne "" } | Out-File Temp ... + ~ An expression was expected after '('. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpectedExpression

The Line

Get-Content $PlaylistName | where {$+.trim() -ne "" } | Out-File Temp.txt

I got from http://www.pixelchef.net/remove-empty-lines-file-powershell

Okay based on the suggestions I now have a partially working script. I got all the files into a text file called Temp.txt using the script as follows:

$PlaylistName = Read-Host 'Playlist filename (Include drive and path):'
$Directory = Read-Host 'Target Directory (Include drive and path):'
Write-Host "Searching "$PlaylistName" for media files and copying to "$Directory"`n"
Get-Content $PlaylistName |  ? { $_ -and (Test-Path $_) } | Out-File Temp.txt

Now I just have to understand how to get the copy-item command to read the Temp.txt file and copy the files into the $Directory folder. My guess is I need to add the line

Get-Content Temp.txt | Copy-Item -Destination $Directory

I hope that is correct. (Still have a lot to learn in PowerShell.)

Jongware
  • 22,200
  • 8
  • 54
  • 100

4 Answers4

2

If you want to get the filename, you can use the Select-String cmdlet with a simple regex to exclude any line starting with a #.

Then you can iterate over the titles and use the GetFileNameWithoutExtension method to retrieve the file names without extensions:

Get-Content c:\test.m3u |
  Select-String '^[^#]' | 
  foreach { [System.IO.Path]::GetFileNameWithoutExtension($_) }

Result example:

A Sign Of The Times
A White Sport Coat (And A Pink Carnation)
Abracadabra
Accidents Never Happen
Addicted To Love
African Killer Bees
Ahab The Arab
Aint Got Rhythm
Ain't No Way To Treat A Lady
Ain't that a Kick in the Head
Ain't That a Shame
Albequerque
Alison
All About That Bass
All Fired Up
All for love ~Rod Stewart_Sting
All I Care About
All I Have To Do Is Dream
All I Wanna Do
All I Want Is You
All My Rowdy Friends Are Coming Over Tonight
All Revved Up With No Place To Go
All Shook Up
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
1

Try this:

 cat .\test.m3u | ? { $_ -and (Test-Path $_)   }

First $_ will skip empty lines, the other one will essentially skip everything not a file.

majkinetor
  • 8,730
  • 9
  • 54
  • 72
  • Okay that helped alot. Now I have the following: `code`$PlaylistName = Read-Host 'Playlist filename (Include drive and path):' $Directory = Read-Host 'Target Directory (Include drive and path):' Write-Host "Searching "$PlaylistName" for media files and copying to "$Directory"`n" Get-Content $PlaylistName | ? { $_ -and (Test-Path $_) } | Out-File Temp.txt `code` Now I just have to understand how to get the copy-item command to read the Temp.txt file and copy the files into the $Directory folder. – Trent Perez Apr 06 '16 at 17:46
0

Okay I got a script that works the way I want it to. It is as follows:

cls
$PlaylistName = Read-Host 'Playlist filename (Include drive and path)'
$Directory = Read-Host 'Target Directory (Include drive and path)'
Write-Host "Searching "$PlaylistName" for media files and copying to "$Directory"`n"
Get-Content $PlaylistName |  ? { $_ -and (Test-Path $_) } | Out-File Temp.txt
Get-Content Temp.txt | Copy-Item -Destination $Directory 
write-host "Operation Completed."

While it is still a little clunky it works. I am wondering if there is a way to display some sort of progress bar during the file copy process or even display the file names as it copies them. Also it would be nice if I could make the script let the user select the playlist name from anywhere on the drive and select the destination directory using windows explorer. I know that stuff is way above my head at the moment but I would love to learn that kind of thing from powershell if it is at all possible. Thanks for all the help so far. I like the idea of displaying the song names without the extension even if I do not need it for the opresent script.l I may have a use for it in the future. Possibly even in an imporvement to this script once I clean it up and make it look "Pretty"

  • In recent PowerShell versions, there is a `Write-Progress` commandlet which can display a progress bar. You'd have to change your script to count how many files there are, loop over them, and both write-progress and copy-item inside the loop. You can prompt for a filename with the Windows standard GUI prompt using `System.Windows.Forms.OpenFileDialog` [like this example](http://www.powershellmagazine.com/2013/07/01/pstip-using-the-system-windows-forms-openfiledialog-class/) – TessellatingHeckler Apr 07 '16 at 00:42
  • Is there a way to strip out just the filename without the path so that I can compare the filenames in the list with the files in the folder for copying new files into the folder at a later time? – Trent Perez Apr 11 '16 at 10:53
0
#Instruction - Create a New folder where you want the tracks to be copied to
#Export the play into the newly created folder as m3u.
#Run this script and browse to the exported playlist txt file
#This script will then copy all the files in that playlist to that folder
#Script written by Clive Foley on 22/06/2017

#Import track details
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "m3u (*.m3u)| *.m3u"
$OpenFileDialog.ShowDialog() | Out-Null


#Import Playlist and collect file path and copy file.
$locations = Get-Content ($OpenFileDialog.filename) | Select-String '^[^#]'

#Set Copy Location
$Des = (split-path ($OpenFileDialog.filename) -Parent)

#Copy tracks
$i=0
foreach ($Track in $locations)
{
copy "$track" "$des"
Write-Progress -activity "Copying $Track"`
-Status "`Tracks copied  = $i" `
-PercentComplete ($i / $loc.count*100)
}