我的递归删除有什么问题?

igb*_*tiz 1 powershell delete-file

Get-ChildItem -recurse | ? {$_.Extension -eq ".obj" } | %{del $_}
                                                           ~~~~~~
CategoryInfo          : ObjectNotFound: (C:\Temp\compilerLimits\template.obj:String)    [Remove-Item], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Run Code Online (Sandbox Code Playgroud)

试图以递归方式删除所有.obj文件; 相反,我明白了.

Kei*_*ill 5

试试这样:

Get-ChildItem -recurse *.obj | Remove-Item
Run Code Online (Sandbox Code Playgroud)

在的情况下| %{del $_},$_是一个System.IO.FileInfo对象,当PowerShell中使用此为LiteralPath参数,它看到的LiteralPath参数需要一个字符串.将FileInfo转换为字符串似乎使用FileInfo.ToString()方法,在某些情况下(如子目录)不包括完整路径 - 只是文件名.这将导致您看到的错误.在FileInfo对象中进行管道传输时,将使用管道参数绑定规则.LiteralPath参数有一个名为PSPath的别名.PowerShell的类型系统将此属性添加到每个FileInfo对象.你可以看到这个Get-ChildItem *.obj | Get-Member.由于LiteralPath参数的ValueFromPipelineByPropertyName设置为true,因此PowerShell将从对象的PSPath属性中获取参数值.

您可以在我的Effective PowerShell电子书的第8项中阅读更多相关信息.