0

我正在我的音乐库中搜索标题与从文件名中提取的歌曲匹配的歌曲。结果搜索非常慢。

ls -Path "C:\Music\New Tracks" | foreach -Process { dir -r -i *.mp3 -Path C:\Music\* | Select-String ([regex]'^.+ - (?<SongTitle>.*)\.mp3$').match($_.Name).Groups[1].Value }

有没有更快的方法来编写脚本?

给出模式的示例文件名是Coldplay Feat Rihanna - Princess Of China.mp3

4

1 回答 1

3

您正在多次运行 C:\Music* 列表 - 对于New Tracks. 我会对此进行一些优化,例如:

$pattern = '^.+ - (?<SongTitle>.*)\.mp3$'
$names = Get-ChildItem 'C:\Music\New Tracks' | 
             Foreach { if ($_.Name -match $pattern) {$matches.SongTitle} }
Get-ChildItem C:\Music -r *.mp3 | 
    Where {$filename = $_.Name; $names | Where {$filename -match $_}}

这假设您在 New Tracks 中的名称少于 MP3 文件,这似乎是合乎逻辑的

于 2012-06-21T19:29:42.677 回答