用于从PowerShell中的m3u文件中提取文件名的脚本

时间:2016-04-05 07:46:29

标签: powershell mp3 m3u

我是powershell的新手,我刚开始学习这个程序的深度。我的问题是我得到了大量的信息,我无法掌握所有这些信息以及如此多的冲突方法,我迷失了。这是我到目前为止所尝试的以及我得到的错误消息。如果有人能告诉我我做错了什么,至少给出了如何修复它的提示,我会非常高兴。提前谢谢。

脚本

$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

播放列表位于:https://gist.github.com/zipster1967/ddc5ce0d81e70f3e59cf0dfb2b224704

当我运行此脚本时,我收到以下错误消息,我不确定它的含义。

  

在第4行:44:44   + Get-Content $ PlaylistName |其中{$ +。trim()-ne“”} | Out-File Temp ......   +〜   在'('。   + CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException   + FullyQualifiedErrorId:ExpectedExpression

The Line

  

Get-Content $ PlaylistName |其中{$ +。trim()-ne“”} | Out-File Temp.txt

我来自http://www.pixelchef.net/remove-empty-lines-file-powershell

好的,根据建议,我现在有一个部分工作的脚本。我使用脚本将所有文件放入名为Temp.txt的文本文件中,如下所示:

$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

现在我只需要了解如何获取copy-item命令来读取Temp.txt文件并将文件复制到$ Directory文件夹中。我的猜测是我需要添加一行

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

我希望这是正确的。 (在PowerShell中还有很多东西需要学习。)

4 个答案:

答案 0 :(得分:2)

如果您想获取文件名,可以使用带有简单正则表达式的Select-String cmdlet排除以#开头的任何行。

然后你可以迭代标题并使用GetFileNameWithoutExtension方法检索没有扩展名的文件名:

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

结果示例:

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

答案 1 :(得分:1)

试试这个:

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

首先$ _将跳过空行,另一个将基本上跳过所有不是文件。

答案 2 :(得分:0)

好的,我的脚本按照我想要的方式工作。它如下:

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."

虽然它仍然有点笨重但它有效。我想知道是否有办法在文件复制过程中显示某种进度条,甚至在复制它们时显示文件名。如果我可以让脚本让用户从驱动器上的任何位置选择播放列表名称并使用Windows资源管理器选择目标目录,那将是很好的。我知道现在的东西已经超出了我的头脑,但是如果可能的话,我很想从powershell那里学到一些东西。感谢目前为止所有的帮助。我喜欢在没有扩展名的情况下显示歌曲名称的想法,即使我不需要它用于opresent script.l我将来也可以使用它。一旦我清理它并使它看起来“漂亮”

,可能甚至会对这个剧本感到不满

答案 3 :(得分:0)

class p(object):
    def __init__(self, name, speed):
        self.name = name
        self.speed = speed

    def get_speed(self):
        return self.speed

order = sorted(uic, key=p.get_speed)
相关问题