繁体   English   中英

PowerShell:递归获取所有子项-没有bin / obj文件夹

[英]PowerShell: get all child items recursively - without bin/obj folders

之后,我想获得多个我要导出(=复制)的源文件夹的所有子项的列表。 但是,我不想获取bin / obj文件夹及其子内容。

到目前为止,我的方法:

Get-ChildItem $RootDirectory -Attributes Directory -Include $includeFilder |
  Get-ChildItem -Recurse -exclude 'bin' |% { Write-Host $_.FullName }

但是,它不起作用。 问题似乎是-exclude 'bin'不匹配,因为整个文件夹名称C:\\Blubb\\bin匹配(例如C:\\Blubb\\bin )。

如何只匹配文件夹名称而不匹配-exclude语句中的整个路径? 是否有更好的方法?

使用@Joey的示例,您可以重新安排一下以避免搜索bin / obj文件夹:

gci $rootdirectory -Directory -Recurse |
   where { $_.FullName -notmatch '\\(bin|obj)(\\|$)' } |
   gci -File -inc $includeFilter | select FullName

只需将-Recurse和where-object过滤器移至第一个GCI,然后将GCI移至每个返回的目录。

您可以更加明确。 并不是很好,但是应该完成工作:

gci $rootdirectory -Directory -inc $includeFilder |
   gci -rec |
   where { $_.FullName -notmatch '\\(bin|obj)(\\|$)' }
   select FullName

您甚至可以删除第二个Get-ChildItem。

可重复使用的过滤器命令:

filter Get-Descendents($Filter={1}) { $_ | where $Filter | foreach { $_; if ($_.PSIsContainer) { $_ | Get-ChildItem | Get-Descendents $Filter } } }

例:

dir C:\code | Get-Descendents { -not $_.PSIsContainer -or $_.Name -notin 'bin', 'obj'}

这不会输入失败过滤器的目录。


它还允许您预定义过滤器并将其作为参数传递。

例:

$myDevItemFilter1 = { -not $_.PSIsContainer -or $_.Name -notin 'bin', 'obj'}
$myDevItemFilter2 = { -not $_.PSIsContainer -or $_.Name -notin '.svn', '.git'}

dir C:\code | Get-Descendents $myDevItemFilter1
dir C:\code | Get-Descendents $myDevItemFilter2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM