简体   繁体   English

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

[英]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.

相关问题 如何在powershell中递归获取所有子项 - How to get all child recursively in powershell 如何递归获取PowerShell、自定义output中所有文件和文件夹的大小 - How to recursively get the size of all files and folders in PowerShell, custom output 如何递归删除 PowerShell 中的所有空文件夹? - How to recursively remove all empty folders in PowerShell? 循环浏览存档中 Outlook 邮箱中的文件夹并获取 powershell 中的所有项目 - Looping through folders in outlook Mailbox in archive and get all the items in powershell Powershell:以递归方式将文件夹中的所有非目录子项都转储到特定路径中 - Powershell: recursively dumping all non-directory child items in a folder into a specific path 如何使用Powershell获取文件夹中所有文件夹名称而不是子文件夹中文件/子文件夹的.txt文件? - How to get a .txt file of all folders names inside a folder but not the files/subfolders inside the child folders using powershell? 在不通过 Powershell 提示用户的情况下递归删除文件夹中的文件 - Recursively deleting files within folders without prompting the user via Powershell 如何以递归方式查找PowerShell中包含.hg文件夹的所有文件夹 - How to recursively find all folders containing a .hg folder in PowerShell 通过PowerShell在XML中获取所有子代,而无需命名标签 - Get all child in xml via powershell without named tag 递归删除带有 Powershell 问题的文件夹 - Deleting folders with Powershell recursively issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM