簡體   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