powershell - 从Powershell中的m3u文件提取文件名的脚本

标签 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

当我运行此脚本时,出现以下错误消息,我不确定这意味着什么。

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



线

Get-Content $PlaylistName | where {$+.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中仍有很多要学习的知识。)

最佳答案

如果要获取文件名,则可以将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

关于powershell - 从Powershell中的m3u文件提取文件名的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36420128/

相关文章:

azure - 在 Azure powershell 中显示自定义错误消息并退出

PowerShell 远程 session 和范围问题 : Commands appear to run locally

Python - 获取mp3的波形/幅度

delphi - 如何在Delphi中播放资源中的mp3内容?

windows - sox 失败实用程序 : Unable to load MAD decoder library (libmad) function "mad_stream_buffer"

html - HTML5 M3U/ASX流播放器

shell - 执行 shell 时 Jenkins 构建失败

azure - ubuntu 受信任存储中的 X.509 证书 - Powershell Microsoft graph

unity-game-engine - 我想从 m3u 文件中提取视频后在 Unity 上播放直播

google-chrome - 如何让 Chrome 在浏览器外*打开 M3U