functions/common/Get-FilteredFiles.ps1
function Get-FilteredFiles { param( [Parameter(Mandatory)][System.IO.FileInfo[]] $Files, [Parameter(Mandatory)][string] $ExcludeListFile, [string] $RootPath = $null ) # Wenn kein RootPath angegeben ist, versuche ihn aus den Files abzuleiten if (-not $RootPath) { # Einfachster Ansatz: gemeinsamer Root der Dateien finden $paths = $Files | ForEach-Object { $_.DirectoryName } $RootPath = Get-CommonRootPath -Paths $paths if (-not $RootPath) { throw "RootPath konnte nicht bestimmt werden. Bitte als Parameter übergeben." } } $RootPath = (Resolve-Path $RootPath).Path.TrimEnd('\','/') # Exclude-Liste laden $excludeEntries = @() if (Test-Path $ExcludeListFile) { try { $excludeEntries = Get-Content $ExcludeListFile | ConvertFrom-Json } catch { Write-Warning "Exclude-Datei konnte nicht gelesen werden: $ExcludeListFile" } } # Dateien und Ordner aus der Exclude-Liste trennen $excludeFilesRelative = @() $excludeDirsRelative = @() foreach ($entry in $excludeEntries) { if ($entry.EndsWith('/')) { $excludeDirsRelative += $entry.TrimEnd('/') } else { $excludeFilesRelative += $entry -Replace '/', '\' } } # Hilfsfunktion um relativen Pfad zu bekommen function Get-RelativePath { param ( [string]$fullPath, [string]$basePath ) # Normiere beide Pfade (Backslashes, kein abschließender Slash) $normFull = [IO.Path]::GetFullPath($fullPath).TrimEnd('\','/') $normBase = [IO.Path]::GetFullPath($basePath).TrimEnd('\','/') + '\' if ($normFull.StartsWith($normBase, [System.StringComparison]::OrdinalIgnoreCase)) { return $normFull.Substring($normBase.Length) } else { throw "Pfad '$fullPath' liegt nicht im Basisverzeichnis '$basePath'." } } $filteredFiles = foreach ($file in $Files) { # relativer Pfad zur Root (mit Backslashes) $relPath = Get-RelativePath -fullPath $file.FullName -basePath $RootPath # Prüfe, ob Datei explizit in excludeFiles ist (relativ) if ($excludeFilesRelative -contains $relPath) { continue } # Prüfe, ob Datei in einem ausgeschlossenen Verzeichnis liegt $inExcludedDir = $false foreach ($exDir in $excludeDirsRelative) { if ($relPath.StartsWith($exDir + '\')) { $inExcludedDir = $true break } } if (-not $inExcludedDir) { $file } } return $filteredFiles } |