Frage

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.

War es hilfreich?

Lösung 2

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
}

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top