AnswerBun.com

Can't exclude a directory pattern recursively

Super User Asked by Steve B on August 14, 2020

I’m trying to get child items of a folder recursively. However the folder contains noise files and folder (actually, this is a Visual Studio project folder).

Here what I have:

$root = Get-Item C:ProjectsMyProject

$allItems =  Get-ChildItem $root -Recurse -exclude "**pkgobj*"

However, $allItems still contains files and folder matching the paths.

What I did wrong?

To be more precise, I want to get both folders and files, but not the specified folder, and any of its descendant.

I also tried:

foreach($item in $allItems){
    if($item.FullName -notmatch "pkgobj") {
        Write-Host -ForegroundColor Green $item.FullName.Replace($root,'')

    }
}

But no more success

2 Answers

Took me a moment to understand the issue. Its a tricky one.

-exclude only applies to basenames of items (i.e. myfile.txt), not the fullname
(i.e. C:pkgobjmyfile.txt) which you want. So you can't use exclude here.

But there is a workaround using Fullname and -notlike

$root = "C:ProjectsMyProject"
$allitems = Get-ChildItem $root -Recurse | Where {$_.FullName -notlike "*pkgobj*"} 

Correct answer by nixda on August 14, 2020

You want to avoid where statements when you can for performance reasons. To simplify a previous answer.

Get-childitem -path c:windows -exclude 'temp' | get-childitem -recurse

Answered by user2291729 on August 14, 2020

Add your own answers!

Related Questions

Solved-How to bulk rename IMG files in Windows

1  Asked on December 5, 2020 by empty_tables

     

Verify which key was used to connect to SSH

1  Asked on December 5, 2020 by ricky

 

Can’t install gcc with cygwin on Windows 10

1  Asked on December 4, 2020 by user69453

     

Potential DNS leak with openVPN on Linux

0  Asked on December 4, 2020 by nox

         

A linux program that uses memory

7  Asked on December 4, 2020 by tony_sid

   

Nested virtualization – VirtualBox on Linux in Hyper-V

0  Asked on December 3, 2020 by jonathan-dahlin

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP