我正在编写一个小的PowerShell脚本,它应该会清理我的Visual项目。我的策略是在子目录中查找所有项目文件(例如*.csproj)。项目文件的父文件夹应该是项目目录。从那里可以很容易地到达bin和obj子文件夹。
通过下面的片段,我想找到所有的项目文件夹:
$dirs = Get-ChildItem -Recurse -File -Path $start_dir -Include *.csproj | select Directory我希望这个语句的计算结果是System.IO.DirectoryInfo对象的序列。令我惊讶的是,事实并非如此。实际上,您得到了一个Selected.System.IO.FileInfo序列:
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对象序列的语句吗?
发布于 2014-02-10 10:01:54
我想你想要这个:
$dirs = Get-ChildItem -Recurse -File -Path $start_dir -Include *.csproj | select -expand Directory | % { [io.directoryinfo]$_ }或
$dirs = [io.directoryinfo[]](Get-ChildItem -Recurse -File -Path $start_dir -Include *.csproj | select -expand Directory )在$dirs中的代码中,您需要使用select-object中的-expand获取[string]值,并将每个值转换为[io.diredtoryinfo]对象。
发布于 2014-02-10 12:52:10
总是生成该对象类型的选定对象。
尝试以下方法获取DirectoryInfo对象的集合:
$dirs = (Get-ChildItem -Recurse -File -Path $start_dir -Include *.csproj).Directoryhttps://stackoverflow.com/questions/21673522
复制相似问题