[英]PowerShell: get all child items recursively - without bin/obj folders
I want to get a list of all child items of a number of source folders I want to export (=copy) afterwards. 之后,我想获得多个我要导出(=复制)的源文件夹的所有子项的列表。 However, I do not want to get the bin/obj folders and their sub contents. 但是,我不想获取bin / obj文件夹及其子内容。
My approach so far: 到目前为止,我的方法:
Get-ChildItem $RootDirectory -Attributes Directory -Include $includeFilder |
Get-ChildItem -Recurse -exclude 'bin' |% { Write-Host $_.FullName }
However, it does not work. 但是,它不起作用。 The problem seems to be that -exclude 'bin'
does not match as the whole folder name down the road (something like C:\\Blubb\\bin
) is matched. 问题似乎是-exclude 'bin'
不匹配,因为整个文件夹名称C:\\Blubb\\bin
匹配(例如C:\\Blubb\\bin
)。
How to match only the folder name and not the whole path in the -exclude
statement? 如何只匹配文件夹名称而不匹配-exclude
语句中的整个路径? Is there maybe an even better approach? 是否有更好的方法?
Using @Joey's example, you can re-arrange that a little to avoid searching the bin/obj folders: 使用@Joey的示例,您可以重新安排一下以避免搜索bin / obj文件夹:
gci $rootdirectory -Directory -Recurse |
where { $_.FullName -notmatch '\\(bin|obj)(\\|$)' } |
gci -File -inc $includeFilter | select FullName
Just move the -Recurse and where-object filter up to the first GCI, then GCI on each of the returned directories. 只需将-Recurse和where-object过滤器移至第一个GCI,然后将GCI移至每个返回的目录。
You can get a little more explicit. 您可以更加明确。 Not terribly nice, but should get the job done: 并不是很好,但是应该完成工作:
gci $rootdirectory -Directory -inc $includeFilder |
gci -rec |
where { $_.FullName -notmatch '\\(bin|obj)(\\|$)' }
select FullName
You could perhaps even drop the second Get-ChildItem. 您甚至可以删除第二个Get-ChildItem。
Reusable filter command: 可重复使用的过滤器命令:
filter Get-Descendents($Filter={1}) { $_ | where $Filter | foreach { $_; if ($_.PSIsContainer) { $_ | Get-ChildItem | Get-Descendents $Filter } } }
Example: 例:
dir C:\code | Get-Descendents { -not $_.PSIsContainer -or $_.Name -notin 'bin', 'obj'}
This won't enter a directory that fails the filter. 这不会输入失败过滤器的目录。
It also allows you to predefine your filters and pass them as parameters. 它还允许您预定义过滤器并将其作为参数传递。
Example: 例:
$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.