首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell: select-object之后的意外序列类型

PowerShell: select-object之后的意外序列类型
EN

Stack Overflow用户
提问于 2014-02-10 09:39:00
回答 2查看 252关注 0票数 0

我正在编写一个小的PowerShell脚本,它应该会清理我的Visual项目。我的策略是在子目录中查找所有项目文件(例如*.csproj)。项目文件的父文件夹应该是项目目录。从那里可以很容易地到达bin和obj子文件夹。

通过下面的片段,我想找到所有的项目文件夹:

代码语言:javascript
复制
$dirs = Get-ChildItem -Recurse -File -Path $start_dir -Include *.csproj | select Directory

我希望这个语句的计算结果是System.IO.DirectoryInfo对象的序列。令我惊讶的是,事实并非如此。实际上,您得到了一个Selected.System.IO.FileInfo序列:

代码语言:javascript
复制
PS> Get-ChildItem -Recurse -File -Path $start_dir -Include *.csproj | select Directory | gm


TypeName: Selected.System.IO.FileInfo

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Directory   NoteProperty System.IO.DirectoryInfo Directory=C:\dev\proj\ART\Sources\ART.Adapter

有人能解释一下这种行为吗?有人知道另一个计算为DirectoryInfo对象序列的语句吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-10 10:01:54

我想你想要这个:

代码语言:javascript
复制
$dirs = Get-ChildItem -Recurse -File -Path $start_dir -Include *.csproj | select -expand Directory | % { [io.directoryinfo]$_ }

代码语言:javascript
复制
$dirs = [io.directoryinfo[]](Get-ChildItem -Recurse -File -Path $start_dir -Include *.csproj | select -expand Directory )

$dirs中的代码中,您需要使用select-object中的-expand获取[string]值,并将每个值转换为[io.diredtoryinfo]对象。

票数 1
EN

Stack Overflow用户

发布于 2014-02-10 12:52:10

总是生成该对象类型的选定对象。

尝试以下方法获取DirectoryInfo对象的集合:

代码语言:javascript
复制
$dirs = (Get-ChildItem -Recurse -File -Path $start_dir -Include *.csproj).Directory
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21673522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档