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

このスクリプトを実行すると、次のエラー メッセージが表示され、意味がわかりません。

行:4 文字:44 + Get-Content $PlaylistName | where {$+.trim() -ne "" } | Out-File Temp ... + ~ '(' の後に式が必要でした。 + 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 について学ぶことはまだたくさんあります。)

4

4 に答える 4

2

ファイル名を取得する場合は、Select-Stringコマンドレットを単純な正規表現と共に使用して、. で始まる行を除外できます#

次に、タイトルを反復処理し、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 $_)   }

最初の $_ は空行をスキップし、もう 1 つは基本的にファイル以外のすべてをスキップします。

于 2016-04-05T08:00:40.710 に答える