开发者

Powershell - Delete all non mp3 files

开发者 https://www.devze.com 2023-01-04 01:13 出处:网络
I´m totally new to Powershell and wanted to write a script that deletes all non-mp3 files in a directory.

I´m totally new to Powershell and wanted to write a script that deletes all non-mp3 files in a directory.

My solution:

get-childitem -Recurse | 
Where-Object {!($_.PSIsContainer)} | 
Where {$_.Extension -ne ".mp3"} | 
remove-item
开发者_高级运维

What can be improved in this statement or could be written in another way. Are there any problems with this statement?

Thank you.


I would use just one Where-Object command:

Get-childitem -Recurse | 
    Where-Object {!$_.PSIsContainer -AND $_.Extension -ne '.mp3'} | 
    Remove-Item -whatIf  

If you're certain that no directories have 'mp3' extension :

Get-childitem -Recurse | Where-Object {$_.Extension -ne '.mp3'} | 
    Remove-Item -whatIf 

Remove -whatIf to delete the files.

0

精彩评论

暂无评论...
验证码 换一张
取 消