Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid recursively fetching specific folders in PowerShell

I have a Visual Studio solution with several projects. I clean up the bin and obj folders as part of clean up using the following script.

Get-ChildItem -path source -filter obj -recurse | Remove-Item -recurse
Get-ChildItem -path source -filter bin -recurse | Remove-Item -recurse

This works perfectly. However, I have a file-based data folder that has about 600,000 sub folders in it and is located in a folder called FILE_DATA.

The above script takes ages because it goes through all these 600,000 folders.

I need to avoid FILE_DATA folder when I recursively traverse and remove bin and obj folders.

like image 207
user1145404 Avatar asked Dec 15 '25 06:12

user1145404


2 Answers

Here is a more efficient approach that does what you need--skip subtrees that you want to exclude:

function GetFiles($path = $pwd, [string[]]$exclude)
{
    foreach ($item in Get-ChildItem $path)
    {
        if ($exclude | Where {$item -like $_}) { continue }

        $item
        if (Test-Path $item.FullName -PathType Container)
        {
            GetFiles $item.FullName $exclude
        }
    }
} 

This code is adapted from Keith Hill's answer to this post; my contribution is one bug fix and minor refactoring; you will find a complete explanation in my answer to that same SO question.

This invocation should do what you need:

GetFiles -path source -exclude FILE_DATA

For even more leisurely reading, check out my article on Simple-Talk.com that discusses this and more: Practical PowerShell: Pruning File Trees and Extending Cmdlets.

like image 139
Michael Sorens Avatar answered Dec 16 '25 21:12

Michael Sorens


If the FILE_DATA folder is a subfolder of $source (not deeper), try:

$source = "C:\Users\Frode\Desktop\test"
Get-Item -Path $source\* -Exclude "FILE_DATA" | ? {$_.PSIsContainer} | % {
    Get-ChildItem -Path $_.FullName -Filter "obj" -Recurse | Remove-Item -Recurse
    Get-ChildItem -Path $_.FullName -Filter "bin" -Recurse | Remove-Item -Recurse
}
like image 43
Frode F. Avatar answered Dec 16 '25 22:12

Frode F.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!