使用带有Remove

编程入门 行业动态 更新时间:2024-10-28 10:23:39
使用带有Remove-Item的Get-ChildItem返回删除的项目数(Return count of items deleted using Get-ChildItem with Remove-Item)

我使用以下命令按预期成功删除文件; 但是,我想知道删除的项目。

Get-ChildItem $dPath -Filter "*.blah" | Remove-Item

我试过这个。 它总是删除文件但是在删除后运行Measure-Object,所以0总是作为计数返回

Get-ChildItem -Path C:\Temp -Filter "*.blah" | Remove-Item | Measure-Object

然后我反过来尝试了它但总是得到:

[Remove-Item],ItemNotFoundException

Get-ChildItem -Path C:\Temp -Filter "*.blah" | Measure-Object | Remove-Item

I am successfully deleting files as expected using the below command; however, I'd like to get a count of the items deleted.

Get-ChildItem $dPath -Filter "*.blah" | Remove-Item

I've tried this. It always deletes the file but the Measure-Object runs after the delete so 0 is always returned as the count

Get-ChildItem -Path C:\Temp -Filter "*.blah" | Remove-Item | Measure-Object

I've then tried it in reverse but always get:

[Remove-Item], ItemNotFoundException

Get-ChildItem -Path C:\Temp -Filter "*.blah" | Measure-Object | Remove-Item

最满意答案

收集变量中的项目,获取该列表的计数,然后删除项目:

$items = Get-ChildItem -Path C:\Temp -Filter "*.blah" $cnt = $items.Count $items | Remove-Item

或计算已成功删除的项目:

$cnt = 0 Get-ChildItem -Path C:\Temp -Filter "*.blah" | ForEach-Object { Remove-Item $_.FullName if ($?) { $cnt++ } }

Collect the items in a variable, get the count of that list, then remove the items:

$items = Get-ChildItem -Path C:\Temp -Filter "*.blah" $cnt = $items.Count $items | Remove-Item

or count the items that were successfully deleted:

$cnt = 0 Get-ChildItem -Path C:\Temp -Filter "*.blah" | ForEach-Object { Remove-Item $_.FullName if ($?) { $cnt++ } }

更多推荐

本文发布于:2023-04-29 06:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335288.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Remove

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!