[英]Remove-Item returns before file is deleted?
在 PowerShell 中,我相信我观察到 PowerShell 试图在删除文件夹内容之前删除文件夹。 我使用的命令是Remove-Item
。
过去,将它与-recurse
一起使用会偶尔出错,抱怨目录不是空的。 当我看到那个错误时,我去检查时会发现目标目录是空的。 我最好的猜测是它正在继续,而文件仍在被删除的过程中。 现在我看到了更多的证据。
由于上述原因,我试图使用以下代码段:
$toDelete = Get-ChildItem $path -Recurse
[Array]::Reverse($toDelete)
$toDelete | Remove-Item
Remove-Item $path
我现在得到的是以下提示:
Confirm
The item at [path to directory] has children and the Recurse parameter was not specified. If you continue, all
children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
同样,当我去检查它时目录是空的。 我最好的猜测是$toDelete | Remove-Item
$toDelete | Remove-Item
在实际删除文件之前返回,并且我在Remove-Item $path
上看到了这个提示。
这是一个已知问题吗? 我对正在发生的事情的怀疑是错误的吗? 我该如何解决?
我尝试使用cmd /C "rd /S [path to directory]"
,但这是有问题的,因为我发现退出代码是0
当它无法删除一些正在使用的孩子时。 (这是我使用的潜在情况,我需要它是一个错误情况。)
带有 -Recurse 开关的 Remove-Item 在 V1-V3 中存在问题(它实际上告诉您它在帮助中无法正常工作)。 它在 V4 中(最终)被修复。 我使用这种方法作为一种解决方法,它总是对我有用:
$toDelete = Get-ChildItem $path -Recurse |
select -ExpandProperty fullname |
sort -Property Length -Descending |
Remove-Item
Remove-Item $path
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.