有没有更快的方式来运行这个Powershell搜索?

时间:2012-06-21 14:54:27

标签: powershell

我正在我的音乐库中搜索标题与从文件名中提取的标题匹配的歌曲。由此产生的搜索速度非常慢。

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

1 个答案:

答案 0 :(得分: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文件似乎是合乎逻辑的