Sunday, October 8, 2023

Get Folders with Exclude folders recursively

In general , we can use get-childitems to get folders recursively and if we want to exculde any list of folders then we can use Exclude switch but this switch wont work for recursively , it only work for current depth so we can use below recursive function to get list of folders recursively by excluding list of folders recursively.

```sh 
function Get-Folders {
    param (
        [string]$Path,
        [string[]]$Excludes
    )
     $list = New-Object System.Collections.Generic.List[string]
    $Directories=(gci $path -Directory -Exclude $Excludes ).FullName

    foreach ( $Directory in $Directories)
    {
      $Directory
      Get-Folders $Directory -Excludes $Excludes
    }
}

[string[]]$Excludes=@("bin","obj",".vscode","dialogs","Controllers","AzureOpenAIBot")

Get-Folders "C:\Users\codeRep\labs\aztd-OpenAI"  -Excludes $Excludes
```

No comments:

Post a Comment