我对 Powershell 完全陌生,想要编写一个删除目录中所有非 mp3 文件的脚本。
我的解决方案:
get-childitem -Recurse |
Where-Object {!($_.PSIsContainer)} |
Where {$_.Extension -ne ".mp3"} |
remove-item
此语句中有哪些可以改进的地方,或者可以用其他方式编写。 这个说法有问题吗?
谢谢。
最佳答案
我只会使用一个 Where-Object
命令:
Get-childitem -Recurse |
Where-Object {!$_.PSIsContainer -AND $_.Extension -ne '.mp3'} |
Remove-Item -whatIf
如果您确定没有目录具有“mp3”扩展名:
Get-childitem -Recurse | Where-Object {$_.Extension -ne '.mp3'} |
Remove-Item -whatIf
删除 -whatIf
以删除文件。
关于Powershell - 删除所有非 mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3086861/