英文:
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
以下是您要翻译的内容:
-
The behavior differs because it's a different kind of object.
行为不同是因为它是不同类型的对象。
-
Files are represented by
FileInfo
objects (which happen to have aDirectory
property), whereas directories are represented byDirectoryInfo
objects (which do not happen to have such a property, but instead has aParent
property for the same purpose).文件由
FileInfo
对象表示(它们恰好具有Directory
属性),而目录由DirectoryInfo
对象表示(它们不具有此属性,但代替具有Parent
属性以达到相同的目的)。 -
You can use
Get-Member
to discover these output types and their members:您可以使用
Get-Member
来查看这些输出类型及其成员: -
If you just want the path to the parent directory, use
Select-Object
withSplit-Path
:如果您只想要父目录的路径,请使用
Select-Object
与Split-Path
: -
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 theAttributes
property:值得注意的是,PowerShell 的提供程序子系统会附加一个名为
PSIsContainer
的 ETS 属性,以指示提供程序项是否为容器类型(例如目录)或叶子类型(例如文件),因此您无需检查Attributes
属性: -
The file system provider also exposes two parameters to
Get-ChildItem
with which you can filter up front:文件系统提供程序还向
Get-ChildItem
暴露了两个参数,您可以使用它们进行前置过滤: -
Get-ChildItem -Directory # get only directories
Get-ChildItem -Directory # 仅获取目录
-
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论