Powershell : Get-ChildItem piped to Select-Object returns null Directory property when selecting directory

huangapple go评论55阅读模式
英文:

Powershell : Get-ChildItem piped to Select-Object returns null Directory property when selecting directory

问题

我正在使用Get-ChildItem命令,带有-Recurse参数来枚举特定路径中的所有文件 和目录

如果我运行:(Get-ChildItem -Path $Path -Recurse | Where-Object -Property Attributes -Value Directory -EQ).Directory,它返回$null

如果我访问相同的文件对象上的Directory属性,它返回搜索路径的相对路径。为什么在目录对象上没有观察到相同的行为?

英文:

I am using the Get-ChildItem cmdlet with the -Recurse parameter to enumerate all files and directories in a specific path.

If I run : (Get-ChildItem -Path $Path -Recurse | Where-Object -Property Attributes -Value Directory -EQ).Directory, it returns $null.

If I access this same Directory property on a file object, this returns the relative path to the search path.
Why isn't the same behaviour observed with a directory object ?

答案1

得分: 2

以下是您要翻译的内容:

  1. The behavior differs because it's a different kind of object.

    行为不同是因为它是不同类型的对象。

  2. Files are represented by FileInfo objects (which happen to have a Directory property), whereas directories are represented by DirectoryInfo objects (which do not happen to have such a property, but instead has a Parent property for the same purpose).

    文件由 FileInfo 对象表示(它们恰好具有 Directory 属性),而目录由 DirectoryInfo 对象表示(它们不具有此属性,但代替具有 Parent 属性以达到相同的目的)。

  3. You can use Get-Member to discover these output types and their members:

    您可以使用 Get-Member 来查看这些输出类型及其成员:

  4. If you just want the path to the parent directory, use Select-Object with Split-Path:

    如果您只想要父目录的路径,请使用 Select-ObjectSplit-Path

  5. It's worth noting that PowerShell's provider subsystem attaches an ETS property named PSIsContainer to indicate whether a provider item is a container type (eg. a directory) or a leaf (eg. a file), so you don't need to inspect the Attributes property:

    值得注意的是,PowerShell 的提供程序子系统会附加一个名为 PSIsContainer 的 ETS 属性,以指示提供程序项是否为容器类型(例如目录)或叶子类型(例如文件),因此您无需检查 Attributes 属性:

  6. The file system provider also exposes two parameters to Get-ChildItem with which you can filter up front:

    文件系统提供程序还向 Get-ChildItem 暴露了两个参数,您可以使用它们进行前置过滤:

  7. Get-ChildItem -Directory # get only directories

    Get-ChildItem -Directory # 仅获取目录

  8. Get-ChildItem -File # get only files

    Get-ChildItem -File # 仅获取文件

英文:

The behavior differs because it's a different kind of object.

Files are represented by FileInfo objects (which happen to have a Directory property), whereas directories are represented by DirectoryInfo objects (which do not happen to have such a property, but instead has a Parent property for the same purpose).

You can use Get-Member to discover these output types and their members:

Get-ChildItem -Path $Path -Recurse |Get-Member

If you just want the path to the parent directory, use Select-Object with Split-Path:

Get-ChildItem -Path $Path -Recurse |Select Name,@{Name='ParentPath';Expression={ $_.FullName |Split-Path -Parent }}

It's worth noting that PowerShell's provider subsystem attaches an ETS property named PSIsContainer to indicate whether a provider item is a container type (eg. a directory) or a leaf (eg. a file), so you don't need to inspect the Attributes property:

Get-ChildItem |Where-Object PSIsContainer -eq $true

The file system provider also exposes two parameters to Get-ChildItem with which you can filter up front:

Get-ChildItem -Directory # get only directories
Get-ChildItem -File      # get only files

huangapple
  • 本文由 发表于 2023年6月26日 22:25:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定