使用带有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然后我反过来尝试了它但总是得到:
Get-ChildItem -Path C:\Temp -Filter "*.blah" | Measure-Object | Remove-Item[Remove-Item],ItemNotFoundException
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-ItemI'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-ObjectI've then tried it in reverse but always get:
Get-ChildItem -Path C:\Temp -Filter "*.blah" | Measure-Object | Remove-Item[Remove-Item], ItemNotFoundException
最满意答案
收集变量中的项目,获取该列表的计数,然后删除项目:
$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-Itemor count the items that were successfully deleted:
$cnt = 0 Get-ChildItem -Path C:\Temp -Filter "*.blah" | ForEach-Object { Remove-Item $_.FullName if ($?) { $cnt++ } }更多推荐
发布评论