1

我对 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

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

在 line:4 char:44 + Get-Content $PlaylistName | 其中 {$+.trim() -ne "" } | Out-File Temp ... + ~ 在 '('. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpectedExpression 之后需要一个表达式

线

获取内容 $PlaylistName | 其中 {$+.trim() -ne "" } | 输出文件 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

4 回答 4

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
于 2016-04-05T08:58:01.783 回答
1

尝试这个:

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

第一个 $_ 将跳过空行,另一个将基本上跳过所有内容而不是文件。

于 2016-04-05T08:00:40.710 回答
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)
} 
于 2017-06-25T07:30:19.730 回答
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 脚本。我将来可能会使用它。一旦我清理它并使它看起来“漂亮”,甚至可能对这个脚本进行改进

于 2016-04-06T19:54:27.173 回答