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.
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.
How to match only the folder name and not the whole path in the -exclude
statement? 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:
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.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.