0

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

到目前为止我的方法:

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

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

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

4

3 回答 3

2

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

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

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

于 2014-04-15T12:25:39.933 回答
0

你可以更明确一点。不是非常好,但应该完成工作:

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

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

于 2014-04-15T11:21:12.780 回答
0

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

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
于 2014-04-15T18:47:43.667 回答