Created
July 17, 2024 17:58
-
-
Save reedjones/d757541bf1c4ae188f922090eae27624 to your computer and use it in GitHub Desktop.
powershell Get files that match the inclusion and exclusion paths and filename
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Search-FilesContent.ps1 | |
param ( | |
[string]$IncludePath = "*", | |
[string]$ExcludePath = "*node_modules*", | |
[string]$Filename = "*", | |
[string[]]$IncludeContent = @(), | |
[string[]]$ExcludeContent = @() | |
) | |
# Get files that match the inclusion and exclusion paths and filename | |
$files = Get-ChildItem -Recurse -Path $IncludePath -Include $Filename -Exclude $ExcludePath -File | |
foreach ($file in $files) { | |
$content = Get-Content $file.FullName | |
# Check if file content includes all specified words | |
$includeMatch = $true | |
foreach ($word in $IncludeContent) { | |
if ($content -notmatch $word) { | |
$includeMatch = $false | |
break | |
} | |
} | |
# Check if file content excludes any specified words | |
$excludeMatch = $false | |
foreach ($word in $ExcludeContent) { | |
if ($content -match $word) { | |
$excludeMatch = $true | |
break | |
} | |
} | |
# Output the file path if it matches the criteria | |
if ($includeMatch -and -not $excludeMatch) { | |
Write-Output $file.FullName | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment